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

View File

@ -0,0 +1,47 @@
set(platform_common_sources emcc.c)
set(platform_gui_libs)
set(platform_libs)
set(CMAKE_EXECUTABLE_SUFFIX ".js")
set(emcc_export_list
# Event handlers for mouse and keyboard input
_mouseup
_mousedown
_mousemove
_key
# Callback when the program activates timing
_timer_callback
# Callback from button presses in the UI outside the canvas
_command
# Game-saving and game-loading functions
_get_save_file
_free_save_file
_load_game
# Callbacks to return values from dialog boxes
_dlg_return_sval
_dlg_return_ival
# Callbacks when the resizing controls are used
_resize_puzzle
_restore_puzzle_size
# Main program, run at initialisation time
_main)
list(TRANSFORM emcc_export_list PREPEND \")
list(TRANSFORM emcc_export_list APPEND \")
string(JOIN "," emcc_export_string ${emcc_export_list})
set(CMAKE_C_LINK_FLAGS "-s ASM_JS=1 -s EXPORTED_FUNCTIONS='[${emcc_export_string}]'")
message("link=${CMAKE_C_LINK_EXECUTABLE}")
set(build_cli_programs FALSE)
function(get_platform_puzzle_extra_source_files OUTVAR NAME)
set(${OUTVAR} PARENT_SCOPE)
endfunction()
function(set_platform_puzzle_target_properties NAME TARGET)
em_link_pre_js(${TARGET} ${CMAKE_SOURCE_DIR}/emccpre.js)
em_link_js_library(${TARGET} ${CMAKE_SOURCE_DIR}/emcclib.js)
endfunction()
function(build_platform_extras)
endfunction()

View File

@ -0,0 +1,60 @@
set(platform_common_sources nestedvm.c printing.c)
set(platform_libs -lm)
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/applet.manifest
"Main-Class: PuzzleApplet\n")
include(FindJava)
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/PuzzleApplet.class
COMMAND ${Java_JAVAC_EXECUTABLE}
-source 1.7 -target 1.7 -d . -cp ${NESTEDVM}/build
${CMAKE_SOURCE_DIR}/PuzzleApplet.java
DEPENDS ${CMAKE_SOURCE_DIR}/PuzzleApplet.java)
function(get_platform_puzzle_extra_source_files OUTVAR NAME)
set(${OUTVAR} PARENT_SCOPE)
endfunction()
function(set_platform_puzzle_target_properties NAME TARGET)
set(build_subdir ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}-tmp)
add_custom_command(OUTPUT ${build_subdir}
COMMAND ${CMAKE_COMMAND} -E make_directory ${build_subdir})
add_custom_command(OUTPUT ${build_subdir}/PuzzleApplet.class
COMMAND ${CMAKE_SOURCE_DIR}/cmake/glob-symlinks.py
${CMAKE_BINARY_DIR} applet.manifest
${CMAKE_BINARY_DIR} PuzzleApplet\\*.class
${NESTEDVM}/build org/ibex/nestedvm/Registers.class
${NESTEDVM}/build org/ibex/nestedvm/UsermodeConstants.class
${NESTEDVM}/build org/ibex/nestedvm/Runtime*.class
${NESTEDVM}/build org/ibex/nestedvm/util/Platform\\*.class
${NESTEDVM}/build org/ibex/nestedvm/util/Seekable\\*.class
WORKING_DIRECTORY ${build_subdir}
DEPENDS
${build_subdir}
${CMAKE_BINARY_DIR}/PuzzleApplet.class
${CMAKE_SOURCE_DIR}/cmake/glob-symlinks.py)
add_custom_command(OUTPUT ${build_subdir}/PuzzleEngine.class
COMMAND ${Java_JAVA_EXECUTABLE}
-cp ${NESTEDVM}/build:${NESTEDVM}/upstream/build/classgen/build
org.ibex.nestedvm.Compiler -outformat class -d .
PuzzleEngine ${CMAKE_CURRENT_BINARY_DIR}/${EXENAME}
DEPENDS
${build_subdir}
${CMAKE_CURRENT_BINARY_DIR}/${EXENAME}
WORKING_DIRECTORY ${build_subdir})
add_custom_target(${TARGET}-jar ALL
COMMAND ${Java_JAR_EXECUTABLE}
cfm ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.jar
applet.manifest PuzzleEngine.class PuzzleApplet*.class org
WORKING_DIRECTORY ${build_subdir}
DEPENDS
${CMAKE_BINARY_DIR}/PuzzleApplet.class
${build_subdir}/PuzzleApplet.class
${build_subdir}/PuzzleEngine.class)
endfunction()
function(build_platform_extras)
endfunction()

58
cmake/platforms/osx.cmake Normal file
View File

@ -0,0 +1,58 @@
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.6)
find_program(HALIBUT halibut REQUIRED)
set(CPACK_GENERATOR DragNDrop)
set(CPACK_PACKAGE_FILE_NAME Puzzles)
set(CPACK_DMG_VOLUME_NAME "Simon Tatham's Puzzle Collection")
include(CPack)
set(build_individual_puzzles FALSE)
function(get_platform_puzzle_extra_source_files OUTVAR NAME)
set(${OUTVAR} PARENT_SCOPE)
endfunction()
function(set_platform_puzzle_target_properties NAME TARGET)
endfunction()
function(build_platform_extras)
write_generated_games_header()
set(resources
${CMAKE_CURRENT_SOURCE_DIR}/osx/Puzzles.icns)
set_source_files_properties(${resources} PROPERTIES
MACOSX_PACKAGE_LOCATION Resources)
add_executable(puzzles MACOSX_BUNDLE
osx.m list.c ${puzzle_sources}
${resources})
set_target_properties(puzzles PROPERTIES
OUTPUT_NAME Puzzles
MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/osx/Info.plist)
target_compile_definitions(puzzles PRIVATE COMBINED)
target_include_directories(puzzles PRIVATE ${generated_include_dir})
target_link_libraries(puzzles common ${platform_gui_libs} ${platform_libs}
"-framework Cocoa")
get_property(bundle_basename TARGET puzzles PROPERTY OUTPUT_NAME)
set(help_dir ${CMAKE_CURRENT_BINARY_DIR}/${bundle_basename}.app/Contents/Resources/Help)
message(${help_dir})
add_custom_command(OUTPUT ${help_dir}
COMMAND ${CMAKE_COMMAND} -E make_directory ${help_dir}
DEPENDS puzzles)
add_custom_command(OUTPUT ${help_dir}/index.html
COMMAND ${HALIBUT} --html
${CMAKE_CURRENT_SOURCE_DIR}/osx-help.but
${CMAKE_CURRENT_SOURCE_DIR}/puzzles.but
DEPENDS
${help_dir}
${CMAKE_CURRENT_SOURCE_DIR}/osx-help.but
${CMAKE_CURRENT_SOURCE_DIR}/puzzles.but
WORKING_DIRECTORY ${help_dir})
add_custom_target(osx_help ALL
DEPENDS ${help_dir}/index.html)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/Puzzles.app
USE_SOURCE_PERMISSIONS
DESTINATION .)
endfunction()

View File

@ -0,0 +1,68 @@
find_package(PkgConfig REQUIRED)
set(PUZZLES_GTK_FOUND FALSE)
macro(try_gtk_package VER PACKAGENAME)
if(NOT PUZZLES_GTK_FOUND AND
(NOT DEFINED PUZZLES_GTK_VERSION OR
PUZZLES_GTK_VERSION STREQUAL ${VER}))
pkg_check_modules(GTK ${PACKAGENAME})
if(GTK_FOUND)
set(PUZZLES_GTK_FOUND TRUE)
endif()
endif()
endmacro()
try_gtk_package(3 gtk+-3.0)
try_gtk_package(2 gtk+-2.0)
if(NOT PUZZLES_GTK_FOUND)
message(FATAL_ERROR "Unable to find any usable version of GTK.")
endif()
include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
set(platform_common_sources gtk.c printing.c)
set(platform_gui_libs ${GTK_LIBRARIES})
set(platform_libs -lm)
set(build_icons TRUE)
function(try_append_cflag flag)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
try_compile(compile_passed ${CMAKE_BINARY_DIR}
SOURCES ${CMAKE_SOURCE_DIR}/cmake/testbuild.c
OUTPUT_VARIABLE test_compile_output
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${GTK_INCLUDE_DIRS}")
if(compile_passed)
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} PARENT_SCOPE)
endif()
endfunction()
if (CMAKE_C_COMPILER_ID MATCHES "GNU" OR
CMAKE_C_COMPILER_ID MATCHES "Clang")
try_append_cflag(-Wall)
try_append_cflag(-Werror)
try_append_cflag(-std=c89)
try_append_cflag(-pedantic)
try_append_cflag(-Wwrite-strings)
endif()
function(get_platform_puzzle_extra_source_files OUTVAR NAME)
if(build_icons AND EXISTS ${CMAKE_SOURCE_DIR}/icons/${NAME}.sav)
build_icon(${NAME})
set(c_icon_file ${CMAKE_BINARY_DIR}/icons/${NAME}-icon.c)
else()
set(c_icon_file ${CMAKE_SOURCE_DIR}/no-icon.c)
endif()
set(${OUTVAR} ${c_icon_file} PARENT_SCOPE)
endfunction()
function(set_platform_puzzle_target_properties NAME TARGET)
install(TARGETS ${TARGET})
endfunction()
function(build_platform_extras)
endfunction()

View File

@ -0,0 +1,40 @@
set(platform_common_sources windows.c printing.c)
set(platform_gui_libs
user32.lib gdi32.lib comctl32.lib comdlg32.lib winspool.lib)
set(platform_libs)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
function(get_platform_puzzle_extra_source_files OUTVAR NAME)
set(${OUTVAR} ${CMAKE_SOURCE_DIR}/puzzles.rc PARENT_SCOPE)
endfunction()
function(set_platform_puzzle_target_properties NAME TARGET)
if(DEFINED ICO_DIR AND EXISTS ${ICO_DIR}/${NAME}.ico)
target_compile_definitions(${TARGET} PRIVATE ICON_FILE=\"${ICO_DIR}/${NAME}.ico\")
endif()
set_target_properties(${TARGET} PROPERTIES WIN32_EXECUTABLE ON)
endfunction()
function(build_platform_extras)
write_generated_games_header()
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/gamedesc.txt "")
list(SORT puzzle_names)
foreach(name ${puzzle_names})
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/gamedesc.txt "\
${name}:\
${exename_${name}}.exe:\
${displayname_${name}}:\
${description_${name}}:\
${objective_${name}}\n")
endforeach()
add_executable(puzzles windows.c list.c ${puzzle_sources})
target_compile_definitions(puzzles PRIVATE COMBINED)
target_include_directories(puzzles PRIVATE ${generated_include_dir})
target_link_libraries(puzzles common ${platform_gui_libs} ${platform_libs})
set_target_properties(puzzles PROPERTIES WIN32_EXECUTABLE ON)
endfunction()