Migrate to a CMake-based build system.

This completely removes the old system of mkfiles.pl + Recipe + .R
files that I used to manage the various per-platform makefiles and
other build scripts in this code base. In its place is a
CMakeLists.txt setup, which is still able to compile for Linux,
Windows, MacOS, NestedVM and Emscripten.

The main reason for doing this is because mkfiles.pl was a horrible
pile of unmaintainable cruft. It was hard to keep up to date (e.g.
didn't reliably support the latest Visual Studio project files); it
was so specific to me that nobody else could maintain it (or was even
interested in trying, and who can blame them?), and it wasn't even
easy to _use_ if you weren't me. And it didn't even produce very good
makefiles.

In fact I've been wanting to hurl mkfiles.pl in the bin for years, but
was blocked by CMake not quite being able to support my clang-cl based
system for cross-compiling for Windows on Linux. But CMake 3.20 was
released this month and fixes the last bug in that area (it had to do
with preprocessing of .rc files), so now I'm unblocked!

CMake is not perfect, but it's better at mkfiles.pl's job than
mkfiles.pl was, and it has the great advantage that lots of other
people already know about it.

Other advantages of the CMake system:

 - Easier to build with. At least for the big three platforms, it's
   possible to write down a list of build commands that's actually the
   same everywhere ("cmake ." followed by "cmake --build ."). There's
   endless scope for making your end-user cmake commands more fancy
   than that, for various advantages, but very few people _have_ to.

 - Less effort required to add a new puzzle. You just add a puzzle()
   statement to the top-level CMakeLists.txt, instead of needing to
   remember eight separate fiddly things to put in the .R file. (Look
   at the reduction in CHECKLST.txt!)

 - The 'unfinished' subdirectory is now _built_ unconditionally, even
   if the things in it don't go into the 'make install' target. So
   they won't bit-rot in future.

 - Unix build: unified the old icons makefile with the main build, so
   that each puzzle builds without an icon, runs to build its icon,
   then relinks with it.

 - Windows build: far easier to switch back and forth between debug
   and release than with the old makefiles.

 - MacOS build: CMake has its own .dmg generator, which is surely
   better thought out than my ten-line bodge.

 - net reduction in the number of lines of code in the code base. In
   fact, that's still true _even_ if you don't count the deletion of
   mkfiles.pl itself - that script didn't even have the virtue of
   allowing everything else to be done exceptionally concisely.
This commit is contained in:
Simon Tatham
2021-03-29 18:23:11 +01:00
parent 72b28b5e71
commit cc7f5503dc
82 changed files with 1202 additions and 3668 deletions

133
cmake/setup.cmake Normal file
View File

@ -0,0 +1,133 @@
set(build_individual_puzzles TRUE)
set(build_cli_programs TRUE)
set(build_icons FALSE)
set(need_c_icons FALSE)
# Include one of platforms/*.cmake to define platform-specific stuff.
# Each of these is expected to:
# - define get_platform_puzzle_extra_source_files(), used below
# - define set_platform_puzzle_target_properties(), used below
# - define build_platform_extras(), called from the top-level CMakeLists.txt
# - override the above build_* settings, if necessary
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
include(cmake/platforms/windows.cmake)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
include(cmake/platforms/osx.cmake)
elseif(CMAKE_SYSTEM_NAME MATCHES "NestedVM")
include(cmake/platforms/nestedvm.cmake)
elseif(CMAKE_C_COMPILER MATCHES "emcc")
include(cmake/platforms/emscripten.cmake)
else() # assume Unix
include(cmake/platforms/unix.cmake)
endif()
# Accumulate lists of the puzzles' bare names and source file
# locations, for use in build_platform_extras() implementations when
# they want to build things based on all the puzzles at once.
set(puzzle_names)
set(puzzle_sources)
include(icons/icons.cmake)
# The main function called from the top-level CMakeLists.txt to define
# each puzzle.
function(puzzle NAME)
cmake_parse_arguments(OPT
"" "DISPLAYNAME;DESCRIPTION;OBJECTIVE;WINDOWS_EXE_NAME" "" ${ARGN})
if(NOT DEFINED OPT_WINDOWS_EXE_NAME)
set(OPT_WINDOWS_EXE_NAME ${NAME})
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(EXENAME ${OPT_WINDOWS_EXE_NAME})
else()
set(EXENAME ${NAME})
endif()
set(exename_${NAME} ${EXENAME} PARENT_SCOPE)
set(displayname_${NAME} ${OPT_DISPLAYNAME} PARENT_SCOPE)
set(description_${NAME} ${OPT_DESCRIPTION} PARENT_SCOPE)
set(objective_${NAME} ${OPT_OBJECTIVE} PARENT_SCOPE)
set(official TRUE)
if(NAME STREQUAL nullgame)
# nullgame is not a playable puzzle; it has to be built (to prove
# it still can build), but not installed, or included in the main
# list of puzzles, or compiled into all-in-one binaries, etc. In
# other words, it's not "officially" part of the puzzle
# collection.
set(official FALSE)
endif()
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR}/unfinished)
# The same goes for puzzles in the 'unfinished' subdirectory,
# although we make an exception if configured to on the command
# line.
list(FIND PUZZLES_ENABLE_UNFINISHED ${NAME} enable_this_one)
if(enable_this_one EQUAL -1)
set(official FALSE)
endif()
endif()
if (official)
set(puzzle_names ${puzzle_names} ${NAME} PARENT_SCOPE)
set(puzzle_sources ${puzzle_sources} ${CMAKE_CURRENT_SOURCE_DIR}/${NAME}.c PARENT_SCOPE)
endif()
get_platform_puzzle_extra_source_files(extra_files ${NAME})
if (build_individual_puzzles)
add_executable(${EXENAME} ${NAME}.c ${extra_files})
target_link_libraries(${EXENAME}
common ${platform_gui_libs} ${platform_libs})
set_platform_puzzle_target_properties(${NAME} ${EXENAME})
endif()
endfunction()
# The main function called from the top-level CMakeLists.txt to define
# a command-line helper tool.
function(cliprogram NAME)
cmake_parse_arguments(OPT
"" "COMPILE_DEFINITIONS" "" ${ARGN})
if(build_cli_programs)
add_executable(${NAME} ${CMAKE_SOURCE_DIR}/nullfe.c
${OPT_UNPARSED_ARGUMENTS})
target_link_libraries(${NAME} common ${platform_libs})
if(OPT_COMPILE_DEFINITIONS)
target_compile_definitions(${NAME} PRIVATE ${OPT_COMPILE_DEFINITIONS})
endif()
endif()
endfunction()
# A small wrapper around cliprogram, taking advantage of the common
# formula that puzzle 'foo' often comes with 'foosolver'.
function(solver NAME)
cliprogram(${NAME}solver ${puzzle_src_prefix}${NAME}.c ${ARGN}
COMPILE_DEFINITIONS STANDALONE_SOLVER)
endfunction()
function(write_generated_games_header)
set(generated_include_dir ${CMAKE_CURRENT_BINARY_DIR}/include)
set(generated_include_dir ${generated_include_dir} PARENT_SCOPE)
file(MAKE_DIRECTORY ${generated_include_dir})
file(WRITE ${generated_include_dir}/generated-games.h "")
list(SORT puzzle_names)
foreach(name ${puzzle_names})
file(APPEND ${generated_include_dir}/generated-games.h "GAME(${name})\n")
endforeach()
endfunction()
# This has to be run from the unfinished subdirectory, so that the
# updates to puzzle_names etc will be propagated to the top-level scope.
macro(export_variables_to_parent_scope)
set(puzzle_names ${puzzle_names} PARENT_SCOPE)
set(puzzle_sources ${puzzle_sources} PARENT_SCOPE)
foreach(name ${puzzle_names})
set(exename_${name} ${exename_${name}} PARENT_SCOPE)
set(displayname_${name} ${displayname_${name}} PARENT_SCOPE)
set(description_${name} ${description_${name}} PARENT_SCOPE)
set(objective_${name} ${objective_${name}} PARENT_SCOPE)
endforeach()
endmacro()