Skip to content

Commit

Permalink
Update: generate Mac bundle on install rather than packaging
Browse files Browse the repository at this point in the history
This is a major overhaul of how OpenTTD is packaged for macOS. Rather
than using a disk image, it's now a Zip archive. The primary
motiviation for this was:

1) Disk images are annoying.
2) Technically, creating the bundle _is_ part of the build.

I noticed that OpenTTD set the working directory as part of its app
launch script. It seemed much simpler to me to simply do this in the
C++ code.

I also added an option for disabling the app bundle "fixup". This
should be useful for developers or packagers such as MacPorts -- which
was how I started looking at this.
  • Loading branch information
danchr committed Jun 5, 2020
1 parent 945508b commit 8e9afdf
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 52 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Expand Up @@ -118,7 +118,8 @@ include_directories(${CMAKE_SOURCE_DIR}/src/3rdparty/squirrel/include)
include(CompileFlags)
compile_flags()

add_executable(openttd WIN32 ${GENERATED_SOURCE_FILES})
add_executable(openttd WIN32 MACOSX_BUNDLE ${GENERATED_SOURCE_FILES})

# All other files are added via target_sources()

include(AddCustomXXXTimestamp)
Expand Down Expand Up @@ -178,6 +179,10 @@ if (APPLE)
-DWITH_COCOA
-DENABLE_COCOA_QUARTZ
)

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/Applications" CACHE PATH "Application installation location" FORCE)
endif()
endif (APPLE)

if (NOT PERSONAL_DIR STREQUAL "(not set)")
Expand Down
8 changes: 4 additions & 4 deletions CPackProperties.cmake.in
Expand Up @@ -4,9 +4,9 @@ set(CPACK_PACKAGE_VERSION "@REV_VERSION@")
# Name the output file with the correct version
string(REPLACE "#CPACK_PACKAGE_VERSION#" "@REV_VERSION@" CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}")

if (CPACK_BUNDLE_PLIST_SOURCE)
if (BUNDLE_PLIST_SOURCE)
# Rewrite the Info.plist.in to contain the correct version
file(READ ${CPACK_BUNDLE_PLIST_SOURCE} INFO_PLIST_CONTENT)
file(READ ${BUNDLE_PLIST_SOURCE} INFO_PLIST_CONTENT)
string(REPLACE "#CPACK_PACKAGE_VERSION#" "@REV_VERSION@" INFO_PLIST_CONTENT "${INFO_PLIST_CONTENT}")
file(WRITE ${CPACK_BUNDLE_PLIST} "${INFO_PLIST_CONTENT}")
endif (CPACK_BUNDLE_PLIST_SOURCE)
file(WRITE ${BUNDLE_PLIST} "${INFO_PLIST_CONTENT}")
endif (BUNDLE_PLIST_SOURCE)
80 changes: 69 additions & 11 deletions cmake/InstallAndPackage.cmake
Expand Up @@ -4,7 +4,12 @@ if (OPTION_INSTALL_FHS)
set(DATA_DESTINATION_DIR "share/games/openttd")
set(DOCS_DESTINATION_DIR "share/doc/openttd")
set(MAN_DESTINATION_DIR "share/man/openttd")
else (OPTION_INSTALL_FHS)
elseif(APPLE)
set(BINARY_DESTINATION_DIR ".")
set(DATA_DESTINATION_DIR "OpenTTD.app/Contents/Resources")
set(DOCS_DESTINATION_DIR "OpenTTD.app/Contents/Resources")
set(MAN_DESTINATION_DIR "OpenTTD.app/Contents/MacOS")
else()
set(BINARY_DESTINATION_DIR ".")
set(DATA_DESTINATION_DIR ".")
set(DOCS_DESTINATION_DIR ".")
Expand All @@ -14,26 +19,33 @@ endif (OPTION_INSTALL_FHS)
install(TARGETS openttd
RUNTIME
DESTINATION ${BINARY_DESTINATION_DIR}
BUNDLE DESTINATION ${BINARY_DESTINATION_DIR}
COMPONENT Runtime
)

install(DIRECTORY
set(DATA_DIRS
${CMAKE_BINARY_DIR}/lang
${CMAKE_BINARY_DIR}/baseset
${CMAKE_SOURCE_DIR}/bin/ai
${CMAKE_SOURCE_DIR}/bin/game
${CMAKE_SOURCE_DIR}/bin/scripts
DESTINATION ${DATA_DESTINATION_DIR}
COMPONENT language_files)
${CMAKE_SOURCE_DIR}/bin/scripts)

install(FILES
set(DOC_FILES
${CMAKE_SOURCE_DIR}/COPYING.md
${CMAKE_SOURCE_DIR}/README.md
${CMAKE_SOURCE_DIR}/changelog.txt
${CMAKE_SOURCE_DIR}/docs/multiplayer.md
${CMAKE_SOURCE_DIR}/known-bugs.txt
DESTINATION ${DOCS_DESTINATION_DIR}
COMPONENT docs)
${CMAKE_SOURCE_DIR}/known-bugs.txt)

if(NOT APPLE)
install(DIRECTORY ${DATA_DIRS}
DESTINATION ${DATA_DESTINATION_DIR}
COMPONENT language_files)

install(FILES ${DOC_FILES}
DESTINATION ${DOCS_DESTINATION_DIR}
COMPONENT docs)
endif(NOT APPLE)

# A Linux manual only makes sense when using FHS. Otherwise it is a very odd
# file with little context to what it is.
Expand Down Expand Up @@ -81,9 +93,54 @@ set(CPACK_STRIP_FILES YES)
set(CPACK_OUTPUT_FILE_PREFIX "bundles")

if (APPLE)
set(CPACK_GENERATOR "Bundle")
include(PackageBundle)
# generate the app bundle as part of the build
string(TIMESTAMP CURRENT_YEAR "%Y")

set_target_properties(openttd PROPERTIES OUTPUT_NAME OpenTTD)
set_target_properties(openttd PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/os/macosx/Info.plist.in")
set_target_properties(openttd PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "OpenTTD")
set_target_properties(openttd PROPERTIES MACOSX_BUNDLE_ICON_FILE "openttd.icns")

add_custom_command(
TARGET openttd POST_BUILD
DEPENDS ${DATA_DIRS} ${DOC_FILES}
COMMAND ${CMAKE_COMMAND} -E make_directory
"${CMAKE_CURRENT_BINARY_DIR}/${DATA_DESTINATION_DIR}")

add_custom_command(
TARGET openttd POST_BUILD
DEPENDS ${DATA_DIRS} ${DOC_FILES}
COMMAND cp -R
${DATA_DIRS} ${DOC_FILES}
${CMAKE_SOURCE_DIR}/os/macosx/openttd.icns
"${CMAKE_CURRENT_BINARY_DIR}/${DATA_DESTINATION_DIR}")

if(OPTION_EMBED_LIBRARIES)
# Delay fixup_bundle() till the install step; this makes sure all executables
# exists and it can do its job.
install(
CODE
"
set(CMAKE_MODULE_PATH \${CMAKE_MODULE_PATH} ${CMAKE_BINARY_DIR})
set(BUNDLE_PATH \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/OpenTTD.app)
set(BUNDLE_PLIST_SOURCE ${CMAKE_BINARY_DIR}/OpenTTD.app/Contents/Info.plist)
set(BUNDLE_PLIST \${BUNDLE_PATH}/Contents/Info.plist)
message(STATUS \"\${BUNDLE_PLIST_SOURCE} -> \${BUNDLE_PLIST}\")
file(COPY ${BUNDLE_PLIST_SOURCE} DESTINATION ${BUNDLE_PLIST_SOURCE}~)
include(CPackProperties)
include(BundleUtilities)
fixup_bundle(\"\${BUNDLE_PATH}/Contents/MacOS/OpenTTD\" \"\" \"\")
"
DESTINATION asdasd
COMPONENT Runtime)
endif(OPTION_EMBED_LIBRARIES)

set(CPACK_GENERATOR "ZIP")
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY NO)
set(CPACK_PACKAGE_FILE_NAME "openttd-#CPACK_PACKAGE_VERSION#-macosx")
elseif (WIN32)
set(CPACK_GENERATOR "ZIP;NSIS")
Expand All @@ -102,6 +159,7 @@ elseif (UNIX)
endif (OPTION_INSTALL_FHS)

set(CPACK_PACKAGE_FILE_NAME "openttd-#CPACK_PACKAGE_VERSION#-linux-${CPACK_SYSTEM_NAME}")

else ()
message(FATAL_ERROR "Unknown OS found for packaging; please consider creating a Pull Request to add support for this OS.")
endif ()
Expand Down
8 changes: 8 additions & 0 deletions cmake/Options.cmake
Expand Up @@ -50,6 +50,10 @@ function(set_options)
option(OPTION_INSTALL_FHS "Install with Filesstem Hierarchy Standard folders" ${DEFAULT_OPTION_INSTALL_FHS})
option(OPTION_USE_ASSERTS "Use assertions; leave enabled for nightlies, betas, and RCs" YES)
option(OPTION_USE_THREADS "Use threads" YES)

if (APPLE)
option(OPTION_EMBED_LIBRARIES "Embed dependent libraries into bundle" YES)
endif (APPLE)
endfunction()

# Show the values of the generic options.
Expand All @@ -61,6 +65,10 @@ function(show_options)
message(STATUS "Option Install FHS - ${OPTION_INSTALL_FHS}")
message(STATUS "Option Use assert - ${OPTION_USE_ASSERTS}")
message(STATUS "Option Use threads - ${OPTION_USE_THREADS}")

if (APPLE)
message(STATUS "Option Embedded dependencies - ${OPTION_EMBED_LIBRARIES}")
endif (APPLE)
endfunction()

# Add the definitions for the options that are selected.
Expand Down
24 changes: 0 additions & 24 deletions cmake/PackageBundle.cmake

This file was deleted.

8 changes: 4 additions & 4 deletions os/macosx/Info.plist.in
Expand Up @@ -6,19 +6,19 @@
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>${CPACK_BUNDLE_NAME}</string>
<string>${MACOSX_BUNDLE_BUNDLE_NAME}</string>
<key>CFBundleExecutable</key>
<string>${CPACK_BUNDLE_NAME}</string>
<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
<key>CFBundleGetInfoString</key>
<string>#CPACK_PACKAGE_VERSION#, Copyright 2004-${CURRENT_YEAR} The OpenTTD team</string>
<key>CFBundleIconFile</key>
<string>${CPACK_BUNDLE_NAME}.icns</string>
<string>${MACOSX_BUNDLE_ICON_FILE}</string>
<key>CFBundleIdentifier</key>
<string>org.openttd.openttd</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${CPACK_BUNDLE_NAME}</string>
<string>${MACOSX_BUNDLE_BUNDLE_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
Expand Down
8 changes: 0 additions & 8 deletions os/macosx/launch.sh

This file was deleted.

4 changes: 4 additions & 0 deletions src/os/unix/unix.cpp
Expand Up @@ -19,6 +19,7 @@


#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
Expand Down Expand Up @@ -250,6 +251,9 @@ int CDECL main(int argc, char *argv[])
if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
argv[1] = nullptr;
argc = 1;

/* change the directory to the application bundle */
chdir(dirname(argv[0]));
}
#endif
CrashLog::InitialiseCrashLog();
Expand Down

0 comments on commit 8e9afdf

Please sign in to comment.