Skip to content

Commit

Permalink
add cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jul 29, 2020
1 parent 6a09ddc commit a3bb2e2
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 28 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci_cmake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: ci_cmake

on:
push:
paths:
- "**/*.f90"
- "**/CMakeLists.txt"
- ".github/workflows/ci_cmake.yml"

jobs:

linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- run: ctest -S setup.cmake -VV
env:
FC: gfortran-9
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
name: fortran_meson
name: ci_meson

on: [push, pull_request]
on:
push:
paths:
- "**/*.f90"
- "**/meson.build"
- ".github/workflows/ci_meson.yml"

jobs:

linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.x'
- run: python -m pip install meson
- run: sudo apt install -yq --no-install-recommends ninja-build gfortran
- run: python -m pip install meson ninja

- run: meson setup build
env:
FC: gfortran
FC: gfortran-9

- run: meson test -C build -v
- uses: actions/upload-artifact@v1
if: failure()
Expand All @@ -26,15 +32,15 @@ jobs:
windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.x'
- run: python -m pip install meson
- run: cinst -y ninja

- run: python -m pip install meson ninja

- run: meson setup build
env:
FC: gfortran

- run: meson test -C build -v
- uses: actions/upload-artifact@v1
if: failure()
Expand All @@ -46,18 +52,20 @@ jobs:
macos:
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.x'
- run: python -m pip install meson
- run: brew install ninja gcc

- run: python -m pip install meson ninja

- run: meson setup build
env:
FC: gfortran
FC: gfortran-9

- run: meson test -C build -v
- uses: actions/upload-artifact@v1
if: failure()
with:
name: Mac_Meson_Testlog
path: build/meson-logs/testlog.txt
path: build/meson-logs/testlog.txt
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.12)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "default build type")
endif()
project(LunarLander1969
LANGUAGES Fortran)
enable_testing()

add_executable(lunar-lander lunar.f90)

find_package(Python3 COMPONENTS Interpreter)

if(Python3_FOUND)
add_test(NAME basic COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/test_stdin.py $<TARGET_FILE:lunar-lander> ${CMAKE_CURRENT_SOURCE_DIR}/good.asc --args='-f\ 16000')
endif()
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Lunar Lander 1969: BASIC and Fortran

[![Actions Status](https://github.com/fortran-gaming/lunar-lander-1969/workflows/fortran_meson/badge.svg)](https://github.com/fortran-gaming/lunar-lander-1969/actions)

![Actions Status](https://github.com/fortran-gaming/lunar-lander-1969/workflows/ci_meson/badge.svg)
![Actions Status](https://github.com/fortran-gaming/lunar-lander-1969/workflows/ci_cmake/badge.svg)

* [Jim Storer](http://www.cs.brandeis.edu/~storer/LunarLander/LunarLander.html), fall 1969: PDP-8 FOCAL
* David H. Ahl, 1973: [BASIC](http://www.cs.brandeis.edu/~storer/LunarLander/LunarLander/Articles/Rocket-101BasicComputerGames.pdf)
Expand All @@ -15,17 +15,15 @@ This game is presented in:
Any Fortran 2008 compliant compiler should work.

```sh
meson build

meson test -C build
ctest -S setup.cmake -VV
```

## Usage

The program defaults to stdin from user.

```sh
./build/lunar
./build/lunar-lander
```

Options:
Expand All @@ -36,9 +34,9 @@ Options:
To mimic the 1969 results

```sh
lunar -f 16000 -d < fail.asc
lunar-lander -f 16000 -d < fail.asc

lunar -f 16000 -d < ok.asc
lunar-lander -f 16000 -d < ok.asc
```

The final output line is formatted for automatic parsing.
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project('Lunar Lander 1969', 'fortran',
default_options : ['default_library=static', 'buildtype=release'])

lunar = executable('lunar', 'lunar.f90',
lunar = executable('lunar-lander', 'lunar.f90',
install: true)

python = find_program('python')
Expand Down
114 changes: 114 additions & 0 deletions setup.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# run by:
# ctest -S setup.cmake

# --- Project-specific -Doptions
# these will be used if the project isn't already configured.
set(_opts)

# --- boilerplate follows
message(STATUS "CMake ${CMAKE_VERSION}")
if(CMAKE_VERSION VERSION_LESS 3.14)
message(FATAL_ERROR "Please update CMake >= 3.14")
endif()

# site is OS name
if(NOT DEFINED CTEST_SITE)
set(CTEST_SITE ${CMAKE_SYSTEM_NAME})
endif()

# parallel test--use ctest_test(PARALLEL_LEVEL ${Ncpu} as setting CTEST_PARALLEL_LEVEL has no effect
include(ProcessorCount)
ProcessorCount(Ncpu)
message(STATUS "${Ncpu} CPU cores detected")

# test name is Fortran compiler in FC
# Note: ctest scripts cannot read cache variables like CMAKE_Fortran_COMPILER
if(DEFINED ENV{FC})
set(FC $ENV{FC})
set(CTEST_BUILD_NAME ${FC})

if(NOT DEFINED ENV{CC})
# use same compiler for C and Fortran, which CMake might not do itself
if(FC STREQUAL ifort)
if(WIN32)
set(ENV{CC} icl)
else()
set(ENV{CC} icc)
endif()
endif()
endif()
endif()

if(NOT DEFINED CTEST_BUILD_CONFIGURATION)
set(CTEST_BUILD_CONFIGURATION "Release")
endif()

set(CTEST_SOURCE_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
if(NOT DEFINED CTEST_BINARY_DIRECTORY)
set(CTEST_BINARY_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/build)
endif()

# CTEST_CMAKE_GENERATOR must be defined in any case here.
if(NOT DEFINED CTEST_CMAKE_GENERATOR)
find_program(_gen NAMES ninja ninja-build samu)
if(_gen)
set(CTEST_CMAKE_GENERATOR "Ninja")
elseif(WIN32)
set(CTEST_CMAKE_GENERATOR "MinGW Makefiles")
set(CTEST_BUILD_FLAGS -j) # not --parallel as this goes to generator directly
else()
set(CTEST_CMAKE_GENERATOR "Unix Makefiles")
set(CTEST_BUILD_FLAGS -j) # not --parallel as this goes to generator directly
endif()
endif()

# -- build and test
ctest_start("Experimental" ${CTEST_SOURCE_DIRECTORY} ${CTEST_BINARY_DIRECTORY})

ctest_configure(
BUILD ${CTEST_BINARY_DIRECTORY}
SOURCE ${CTEST_SOURCE_DIRECTORY}
OPTIONS "${_opts}"
RETURN_VALUE return_code
CAPTURE_CMAKE_ERROR cmake_err)

# if it's a generator or compiler mismatch, delete cache and try again
if(NOT cmake_err EQUAL 0)
file(REMOVE ${CTEST_BINARY_DIRECTORY}/CMakeCache.txt)

ctest_configure(
BUILD ${CTEST_BINARY_DIRECTORY}
SOURCE ${CTEST_SOURCE_DIRECTORY}
OPTIONS "${_opts}"
RETURN_VALUE return_code
CAPTURE_CMAKE_ERROR cmake_err)
endif()

if(return_code EQUAL 0 AND cmake_err EQUAL 0)
ctest_build(
BUILD ${CTEST_BINARY_DIRECTORY}
CONFIGURATION ${CTEST_BUILD_CONFIGURATION}
RETURN_VALUE return_code
NUMBER_ERRORS Nerror
CAPTURE_CMAKE_ERROR cmake_err
)
else()
message(STATUS "SKIP: ctest_build(): returncode: ${return_code}; CMake error code: ${cmake_err}")
endif()

if(return_code EQUAL 0 AND Nerror EQUAL 0 AND cmake_err EQUAL 0)
ctest_test(
BUILD ${CTEST_BINARY_DIRECTORY}
RETURN_VALUE return_code
CAPTURE_CMAKE_ERROR ctest_err
PARALLEL_LEVEL ${Ncpu}
)
else()
message(STATUS "SKIP: ctest_test(): returncode: ${return_code}; CMake error code: ${cmake_err}")
endif()

# ctest_submit()

if(NOT (return_code EQUAL 0 AND Nerror EQUAL 0 AND cmake_err EQUAL 0 AND ctest_err EQUAL 0))
message(FATAL_ERROR "Build and test failed.")
endif()

0 comments on commit a3bb2e2

Please sign in to comment.