Add: [Script] Custom news message text for industry SetProductionLevel.
[openttd-github.git] / cmake / InstallAndPackage.cmake
blob3a894b5a62507564c3f4dd358e1700dbc92d935f
1 include(GNUInstallDirs)
3 # If requested, use FHS layout; otherwise fall back to a flat layout.
4 if(OPTION_INSTALL_FHS)
5     set(BINARY_DESTINATION_DIR "${CMAKE_INSTALL_BINDIR}")
6     set(DATA_DESTINATION_DIR "${CMAKE_INSTALL_DATADIR}/${BINARY_NAME}")
7     set(DOCS_DESTINATION_DIR "${CMAKE_INSTALL_DOCDIR}")
8     set(MAN_DESTINATION_DIR "${CMAKE_INSTALL_MANDIR}")
9 else()
10     if(APPLE)
11         set(BINARY_DESTINATION_DIR "../MacOS")
12     else()
13         set(BINARY_DESTINATION_DIR ".")
14     endif()
15     set(DATA_DESTINATION_DIR ".")
16     set(DOCS_DESTINATION_DIR ".")
17     set(MAN_DESTINATION_DIR ".")
18 endif()
20 install(TARGETS openttd
21         RUNTIME
22             DESTINATION ${BINARY_DESTINATION_DIR}
23             COMPONENT Runtime
24         )
26 install(DIRECTORY
27                 ${CMAKE_BINARY_DIR}/lang
28                 ${CMAKE_BINARY_DIR}/baseset
29                 ${CMAKE_BINARY_DIR}/ai
30                 ${CMAKE_BINARY_DIR}/game
31                 ${CMAKE_SOURCE_DIR}/bin/scripts
32         DESTINATION ${DATA_DESTINATION_DIR}
33         COMPONENT language_files
34         REGEX "ai/[^\.]+$" EXCLUDE # Ignore subdirs in ai dir
37 install(FILES
38                 ${CMAKE_SOURCE_DIR}/COPYING.md
39                 ${CMAKE_SOURCE_DIR}/README.md
40                 ${CMAKE_SOURCE_DIR}/changelog.txt
41                 ${CMAKE_SOURCE_DIR}/docs/multiplayer.md
42                 ${CMAKE_SOURCE_DIR}/known-bugs.txt
43         DESTINATION ${DOCS_DESTINATION_DIR}
44         COMPONENT docs)
46 # A Linux manual only makes sense when using FHS. Otherwise it is a very odd
47 # file with little context to what it is.
48 if(OPTION_INSTALL_FHS)
49     set(MAN_SOURCE_FILE ${CMAKE_SOURCE_DIR}/docs/openttd.6)
50     set(MAN_BINARY_FILE ${CMAKE_BINARY_DIR}/docs/${BINARY_NAME}.6)
51     install(CODE
52             "
53                 execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${MAN_SOURCE_FILE} ${MAN_BINARY_FILE})
54                 execute_process(COMMAND gzip -9 -n -f ${MAN_BINARY_FILE})
55             "
56             COMPONENT manual)
57     install(FILES
58                     ${MAN_BINARY_FILE}.gz
59             DESTINATION ${MAN_DESTINATION_DIR}/man6
60             COMPONENT manual)
61 endif()
63 if(UNIX AND NOT APPLE)
64     install(DIRECTORY
65                     ${CMAKE_BINARY_DIR}/media/icons
66                     ${CMAKE_BINARY_DIR}/media/pixmaps
67             DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}
68             COMPONENT media)
70     install(FILES
71                     ${CMAKE_BINARY_DIR}/media/${BINARY_NAME}.desktop
72             DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications
73             COMPONENT menu)
74 endif()
76 if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
77     set(ARCHITECTURE "amd64")
78 else()
79     string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" ARCHITECTURE)
80 endif()
82 # Windows is a bit more annoying to detect; using the size of void pointer
83 # seems to be the most robust.
84 if(WIN32)
85     # Check if the MSVC platform has been defined
86     if ("$ENV{Platform}" STREQUAL "arm64")
87         set(ARCHITECTURE "arm64")
88     else()
89         if(CMAKE_SIZEOF_VOID_P EQUAL 8)
90             set(ARCHITECTURE "win64")
91         else()
92             set(ARCHITECTURE "win32")
93         endif()
94     endif()
95 endif()
96 if(APPLE AND CMAKE_OSX_ARCHITECTURES)
97     string(TOLOWER "${CMAKE_OSX_ARCHITECTURES}" ARCHITECTURE)
98 endif()
100 set(CPACK_SYSTEM_NAME "${ARCHITECTURE}")
102 set(CPACK_PACKAGE_NAME "openttd")
103 set(CPACK_PACKAGE_VENDOR "OpenTTD")
104 set(CPACK_PACKAGE_DESCRIPTION "OpenTTD")
105 set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "OpenTTD")
106 set(CPACK_PACKAGE_HOMEPAGE_URL "https://www.openttd.org/")
107 set(CPACK_PACKAGE_CONTACT "OpenTTD <info@openttd.org>")
108 set(CPACK_PACKAGE_INSTALL_DIRECTORY "OpenTTD")
109 set(CPACK_PACKAGE_CHECKSUM "SHA256")
111 if((APPLE OR WIN32) AND EXISTS ${PANDOC_EXECUTABLE})
112     execute_process(COMMAND ${PANDOC_EXECUTABLE} "${CMAKE_SOURCE_DIR}/COPYING.md" -s -o "${CMAKE_BINARY_DIR}/COPYING.rtf")
113     set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_BINARY_DIR}/COPYING.rtf")
114 else()
115     set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING.md")
116 endif()
118 set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
119 set(CPACK_MONOLITHIC_INSTALL YES)
120 set(CPACK_PACKAGE_EXECUTABLES "openttd;OpenTTD")
121 set(CPACK_STRIP_FILES YES)
122 set(CPACK_OUTPUT_FILE_PREFIX "bundles")
124 if(APPLE)
125     # Stripping would produce unreadable stacktraces.
126     set(CPACK_STRIP_FILES NO)
127     set(CPACK_GENERATOR "Bundle")
128     include(PackageBundle)
130     if (APPLE_UNIVERSAL_PACKAGE)
131         set(CPACK_PACKAGE_FILE_NAME "openttd-#CPACK_PACKAGE_VERSION#-macos-universal")
132     else()
133         set(CPACK_PACKAGE_FILE_NAME "openttd-#CPACK_PACKAGE_VERSION#-macos-${CPACK_SYSTEM_NAME}")
134     endif()
135 elseif(WIN32)
136     set(CPACK_GENERATOR "ZIP")
137     if(OPTION_USE_NSIS)
138         list(APPEND CPACK_GENERATOR "NSIS")
139         include(PackageNSIS)
140     endif()
142     set(CPACK_PACKAGE_FILE_NAME "openttd-#CPACK_PACKAGE_VERSION#-windows-${CPACK_SYSTEM_NAME}")
144     if(WINDOWS_CERTIFICATE_COMMON_NAME)
145       add_custom_command(TARGET openttd
146         POST_BUILD
147         COMMAND "${CMAKE_SOURCE_DIR}/os/windows/sign.bat" "$<TARGET_FILE:openttd>" "${WINDOWS_CERTIFICATE_COMMON_NAME}"
148       )
149     endif()
150 elseif(UNIX)
151     # With FHS, we can create deb/rpm/... Without it, they would be horribly broken
152     # and not work. The other way around is also true; with FHS they are not
153     # usable, and only flat formats work.
154     if(OPTION_PACKAGE_DEPENDENCIES)
155         set(CPACK_GENERATOR "TXZ")
156         set(PLATFORM "generic")
157     elseif(NOT OPTION_INSTALL_FHS)
158         set(CPACK_GENERATOR "TXZ")
159         set(PLATFORM "unknown")
160     else()
161         find_program(LSB_RELEASE_EXEC lsb_release)
162         execute_process(COMMAND ${LSB_RELEASE_EXEC} -is
163             OUTPUT_VARIABLE LSB_RELEASE_ID
164             OUTPUT_STRIP_TRAILING_WHITESPACE
165         )
166         if(LSB_RELEASE_ID)
167             if(LSB_RELEASE_ID STREQUAL "Ubuntu" OR LSB_RELEASE_ID STREQUAL "Debian")
168                 execute_process(COMMAND ${LSB_RELEASE_EXEC} -cs
169                     OUTPUT_VARIABLE LSB_RELEASE_CODENAME
170                     OUTPUT_STRIP_TRAILING_WHITESPACE
171                 )
172                 string(TOLOWER "${LSB_RELEASE_ID}-${LSB_RELEASE_CODENAME}" PLATFORM)
174                 set(CPACK_GENERATOR "DEB")
175                 include(PackageDeb)
176             elseif(LSB_RELEASE_ID STREQUAL "Fedora")
177                 set(PLATFORM "fedora")
178                 set(CPACK_GENERATOR "RPM")
179                 include(PackageRPM)
180             else()
181                 set(UNSUPPORTED_PLATFORM_NAME "LSB-based Linux distribution '${LSB_RELEASE_ID}'")
182             endif()
183         elseif(EXISTS "/etc/os-release")
184             file(STRINGS "/etc/os-release" OS_RELEASE_CONTENTS REGEX "^ID=")
185             string(REGEX MATCH "ID=(.*)" _ ${OS_RELEASE_CONTENTS})
186             set(DISTRO_ID ${CMAKE_MATCH_1})
187             if(DISTRO_ID STREQUAL "arch")
188                 set(PLATFORM "arch")
189                 set(CPACK_GENERATOR "TXZ")
190             elseif(DISTRO_ID STREQUAL "fedora" OR DISTRO_ID STREQUAL "rhel")
191                 set(PLATFORM "fedora")
192                 set(CPACK_GENERATOR "RPM")
193                 include(PackageRPM)
194             else()
195                 set(UNSUPPORTED_PLATFORM_NAME "Linux distribution '${DISTRO_ID}' from /etc/os-release")
196             endif()
197         else()
198             set(UNSUPPORTED_PLATFORM_NAME "Linux distribution")
199         endif()
201         if(NOT PLATFORM)
202             set(PLATFORM "generic")
203             set(CPACK_GENERATOR "TXZ")
204             message(WARNING "Unknown ${UNSUPPORTED_PLATFORM_NAME} found for packaging; can only pack to a txz. Please consider creating a Pull Request to add support for this distribution.")
205         endif()
206     endif()
208     set(CPACK_PACKAGE_FILE_NAME "openttd-#CPACK_PACKAGE_VERSION#-linux-${PLATFORM}-${CPACK_SYSTEM_NAME}")
210 else()
211     message(FATAL_ERROR "Unknown OS found for packaging; please consider creating a Pull Request to add support for this OS.")
212 endif()
214 include(CPack)
216 if(OPTION_PACKAGE_DEPENDENCIES)
217     # Install all dependencies we can resolve, with the exception of ones that
218     # every Linux machine should have. This should make this build as generic
219     # as possible, where it runs on any machine with the same or newer libc
220     # than the one this is compiled with.
221     # We copy these libraries into lib/ folder, so they can be found on game
222     # startup. See comment in root CMakeLists.txt for how this works exactly.
223     install(CODE [[
224         file(GET_RUNTIME_DEPENDENCIES
225                 RESOLVED_DEPENDENCIES_VAR DEPENDENCIES
226                 UNRESOLVED_DEPENDENCIES_VAR UNRESOLVED_DEPENDENCIES
227                 EXECUTABLES openttd
228                 POST_EXCLUDE_REGEXES "ld-linux|libc.so|libdl.so|libm.so|libgcc_s.so|libpthread.so|librt.so|libstdc...so")
229         file(INSTALL
230                 DESTINATION "${CMAKE_INSTALL_PREFIX}/lib"
231                 FILES ${DEPENDENCIES}
232                 FOLLOW_SYMLINK_CHAIN)
234         # This should not be possible, but error out when a dependency cannot
235         # be resolved.
236         list(LENGTH UNRESOLVED_DEPENDENCIES UNRESOLVED_LENGTH)
237         if(${UNRESOLVED_LENGTH} GREATER 0)
238             message(FATAL_ERROR "Unresolved dependencies: ${UNRESOLVED_DEPENDENCIES}")
239         endif()
240     ]])
241 endif()