-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
CMakeLists.txt
426 lines (352 loc) · 13.2 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Z80 - CMakeLists.txt
# ______ ______ ______
# /\___ \/\ __ \\ __ \
# \/__/ /\_\ __ \\ \/\ \
# /\_____\\_____\\_____\
# Zilog \/_____//_____//_____/ CPU Emulator
# Copyright (C) 1999-2024 Manuel Sainz de Baranda y Goñi.
# Released under the terms of the GNU Lesser General Public License v3.
cmake_minimum_required(
# 3.14: install(TARGETS ARCHIVE [DESTINATION])
VERSION 3.14...${CMAKE_VERSION})
if( CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR AND
NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES
)
set(CMAKE_BUILD_TYPE Release)
endif()
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/API/Z80.h" _)
string(REGEX MATCH ".*Z80_LIBRARY_VERSION_STRING \"([^\n]*)\".*" _ ${_})
project(Z80
VERSION ${CMAKE_MATCH_1}
LANGUAGES C
DESCRIPTION "Zilog Z80 CPU emulator"
HOMEPAGE_URL "https://zxe.io/software/Z80")
unset(_)
message("${PROJECT_NAME} v${PROJECT_VERSION}")
include(GNUInstallDirs)
set(${PROJECT_NAME}_DEPOT_LOCATION
"http://zxe.io/depot" CACHE STRING
"${PROJECT_NAME}: \
Directory or URL of the depot containing the test files.")
option(${PROJECT_NAME}_FETCH_TEST_FILES
"${PROJECT_NAME}: \
Copy or download the test files from the depot to the build directory."
NO)
set(${PROJECT_NAME}_INSTALL_CMAKEDIR
"${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE STRING
"${PROJECT_NAME}: \
Directory in which to install the CMake config-file package.")
set(${PROJECT_NAME}_INSTALL_PKGCONFIGDIR
"${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE STRING
"${PROJECT_NAME}: \
Directory in which to install the pkg-config file.")
set(${PROJECT_NAME}_NOSTDLIB_FLAGS
"Auto" CACHE STRING
"${PROJECT_NAME}: \
Linker flags used to avoid linking against system libraries.")
option(${PROJECT_NAME}_OBJECT_LIBS
"${PROJECT_NAME}: \
Build the emulator as an object library."
NO)
set(${PROJECT_NAME}_SPHINX_HTML_THEME
"" CACHE STRING
"${PROJECT_NAME}: \
Sphinx theme for the documentation in HTML format.")
option(${PROJECT_NAME}_WITH_CMAKE_SUPPORT
"${PROJECT_NAME}: \
Generate and install the CMake config-file package."
NO)
option(${PROJECT_NAME}_WITH_HTML_DOCUMENTATION
"${PROJECT_NAME}: \
Build and install the documentation in HTML format."
NO)
option(${PROJECT_NAME}_WITH_PDF_DOCUMENTATION
"${PROJECT_NAME}: \
Build and install the documentation in PDF format."
NO)
option(${PROJECT_NAME}_WITH_PKGCONFIG_SUPPORT
"${PROJECT_NAME}: \
Generate and install the pkg-config file."
NO)
option(${PROJECT_NAME}_WITH_STANDARD_DOCUMENTS
"${PROJECT_NAME}: \
Install the standard text documents distributed with the package: \
AUTHORS, COPYING, COPYING.LESSER, HISTORY, README and THANKS."
NO)
option(${PROJECT_NAME}_WITH_TESTS
"${PROJECT_NAME}: \
Build the testing tool."
NO)
option(${PROJECT_NAME}_WITH_EXECUTE
"${PROJECT_NAME}: \
Build the implementation of the `z80_execute` function."
NO)
option(${PROJECT_NAME}_WITH_FULL_IM0
"${PROJECT_NAME}: \
Build the full implementation of the interrupt mode 0 rather than the \
reduced one."
NO)
option(${PROJECT_NAME}_WITH_IM0_RETX_NOTIFICATIONS
"${PROJECT_NAME}: \
Enable optional notifications for any `reti` or `retn` instruction \
executed during the interrupt mode 0 response."
NO)
option(${PROJECT_NAME}_WITH_PARITY_COMPUTATION
"${PROJECT_NAME}: \
Enable actual parity calculation for the P/V flag instead of using a \
table of precomputed values (this is for benchmarks, DO NOT ENABLE in \
production builds)."
NO)
option(${PROJECT_NAME}_WITH_Q
"${PROJECT_NAME}: \
Build the implementation of Q."
NO)
option(${PROJECT_NAME}_WITH_SPECIAL_RESET
"${PROJECT_NAME}: \
Build the implementation of the special RESET."
NO)
option(${PROJECT_NAME}_WITH_UNOFFICIAL_RETI
"${PROJECT_NAME}: \
Configure the undocumented instructions ED5Dh, ED6Dh and ED7Dh as `reti` \
instead of `retn`."
NO)
option(${PROJECT_NAME}_WITH_ZILOG_NMOS_LD_A_IR_BUG
"${PROJECT_NAME}: \
Build the implementation of the bug affecting the Zilog Z80 NMOS, which \
causes the P/V flag to be reset when a maskable interrupt is accepted \
during the execution of the `ld a,{i|r}` instructions."
NO)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake")
find_package(Zeta REQUIRED)
if(${PROJECT_NAME}_OBJECT_LIBS)
add_library(${PROJECT_NAME} OBJECT)
else()
if(DEFINED ${PROJECT_NAME}_SHARED_LIBS)
set(BUILD_SHARED_LIBS ${${PROJECT_NAME}_SHARED_LIBS})
endif()
add_library(${PROJECT_NAME})
endif()
target_sources(
${PROJECT_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/API/Z80.h"
"${CMAKE_CURRENT_SOURCE_DIR}/sources/Z80.c")
set_target_properties(
${PROJECT_NAME} PROPERTIES
C_STANDARD 99
C_STANDARD_REQUIRED NO
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
DEBUG_POSTFIX "-debug"
PUBLIC_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/API/Z80.h")
target_link_libraries(${PROJECT_NAME} PUBLIC Zeta)
target_include_directories(
${PROJECT_NAME} PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/API>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
if(${PROJECT_NAME}_OBJECT_LIBS OR NOT BUILD_SHARED_LIBS)
target_compile_definitions(${PROJECT_NAME} PUBLIC Z80_STATIC)
else()
if(WIN32)
if(PROJECT_VERSION_PATCH STREQUAL "")
set(PROJECT_VERSION_PATCH 0)
endif()
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/sources/Z80.rc.in"
"${CMAKE_CURRENT_BINARY_DIR}/Z80.rc"
@ONLY)
target_sources(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/Z80.rc")
endif()
if(${PROJECT_NAME}_NOSTDLIB_FLAGS STREQUAL "Auto")
if(MSVC)
target_compile_definitions(${PROJECT_NAME} PRIVATE Z80_WITH_WINDOWS_DLL_MAIN)
target_link_options(${PROJECT_NAME} PRIVATE "/NODEFAULTLIB")
elseif(APPLE)
if(CMAKE_C_COMPILER_ID MATCHES "^(AppleClang|Clang|GNU)$")
target_link_options(${PROJECT_NAME} PRIVATE "LINKER:-dead_strip_dylibs")
endif()
elseif(CMAKE_C_COMPILER_ID MATCHES "^(ARMClang|Clang|GNU|Intel|TinyCC|XL|XLClang)$")
target_link_options(${PROJECT_NAME} PRIVATE "-nostdlib")
elseif(CMAKE_C_COMPILER_ID MATCHES "PGI")
target_link_options(${PROJECT_NAME} PRIVATE "-Mnostdlib")
elseif(CMAKE_C_COMPILER_ID MATCHES "SunPro")
target_link_options(${PROJECT_NAME} PRIVATE "-xnolib")
endif()
elseif(NOT ${PROJECT_NAME}_NOSTDLIB_FLAGS STREQUAL "")
target_link_options(${PROJECT_NAME} PRIVATE ${${PROJECT_NAME}_NOSTDLIB_FLAGS})
endif()
endif()
target_compile_definitions(
${PROJECT_NAME} PRIVATE
$<$<BOOL:${${PROJECT_NAME}_WITH_EXECUTE}>:Z80_WITH_EXECUTE>
$<$<BOOL:${${PROJECT_NAME}_WITH_FULL_IM0}>:Z80_WITH_FULL_IM0>
$<$<BOOL:${${PROJECT_NAME}_WITH_IM0_RETX_NOTIFICATIONS}>:Z80_WITH_IM0_RETX_NOTIFICATIONS>
$<$<BOOL:${${PROJECT_NAME}_WITH_PARITY_COMPUTATION}>:Z80_WITH_PARITY_COMPUTATION>
$<$<BOOL:${${PROJECT_NAME}_WITH_Q}>:Z80_WITH_Q>
$<$<BOOL:${${PROJECT_NAME}_WITH_SPECIAL_RESET}>:Z80_WITH_SPECIAL_RESET>
$<$<BOOL:${${PROJECT_NAME}_WITH_UNOFFICIAL_RETI}>:Z80_WITH_UNOFFICIAL_RETI>
$<$<BOOL:${${PROJECT_NAME}_WITH_ZILOG_NMOS_LD_A_IR_BUG}>:Z80_WITH_ZILOG_NMOS_LD_A_IR_BUG>)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}_Targets
RUNTIME COMPONENT ${PROJECT_NAME}_Runtime
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
COMPONENT ${PROJECT_NAME}_Runtime
NAMELINK_COMPONENT ${PROJECT_NAME}_Development
ARCHIVE COMPONENT ${PROJECT_NAME}_Development
PUBLIC_HEADER
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
COMPONENT ${PROJECT_NAME}_Development)
if( NOT ${PROJECT_NAME}_OBJECT_LIBS AND
(${PROJECT_NAME}_WITH_CMAKE_SUPPORT OR ${PROJECT_NAME}_WITH_PKGCONFIG_SUPPORT)
)
include(CMakePackageConfigHelpers)
if(${PROJECT_NAME}_WITH_CMAKE_SUPPORT)
if(BUILD_SHARED_LIBS)
set(_type Shared)
else()
set(_type Static)
endif()
install(EXPORT ${PROJECT_NAME}_Targets
DESTINATION "${${PROJECT_NAME}_INSTALL_CMAKEDIR}"
FILE ${PROJECT_NAME}${_type}Targets.cmake
COMPONENT ${PROJECT_NAME}_Development)
unset(_type)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/support/${PROJECT_NAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${${PROJECT_NAME}_INSTALL_CMAKEDIR}")
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION "${${PROJECT_NAME}_INSTALL_CMAKEDIR}"
COMPONENT ${PROJECT_NAME}_Development)
endif()
if(${PROJECT_NAME}_WITH_PKGCONFIG_SUPPORT)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/support/${PROJECT_NAME}.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
@ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc"
DESTINATION "${${PROJECT_NAME}_INSTALL_PKGCONFIGDIR}"
COMPONENT ${PROJECT_NAME}_Development)
endif()
endif()
if(${PROJECT_NAME}_WITH_STANDARD_DOCUMENTS)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/AUTHORS"
"${CMAKE_CURRENT_SOURCE_DIR}/COPYING"
"${CMAKE_CURRENT_SOURCE_DIR}/COPYING.LESSER"
"${CMAKE_CURRENT_SOURCE_DIR}/HISTORY"
"${CMAKE_CURRENT_SOURCE_DIR}/README"
"${CMAKE_CURRENT_SOURCE_DIR}/THANKS"
DESTINATION "${CMAKE_INSTALL_DOCDIR}"
COMPONENT ${PROJECT_NAME}_Runtime)
endif()
if(${PROJECT_NAME}_WITH_HTML_DOCUMENTATION OR ${PROJECT_NAME}_WITH_PDF_DOCUMENTATION)
add_subdirectory(documentation)
endif()
if(${PROJECT_NAME}_WITH_TESTS)
include(CTest)
find_package(ZLIB QUIET)
find_package(libzip QUIET)
add_executable(test-${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/sources/test-Z80.c")
target_link_libraries(test-${PROJECT_NAME} PRIVATE ${PROJECT_NAME})
if(ZLIB_FOUND AND libzip_FOUND)
target_link_libraries(test-${PROJECT_NAME} PRIVATE ZLIB::ZLIB libzip::zip)
target_compile_definitions(test-${PROJECT_NAME} PRIVATE TEST_Z80_WITH_ARCHIVE_EXTRACTION)
endif()
if(${PROJECT_NAME}_WITH_EXECUTE)
target_compile_definitions(test-${PROJECT_NAME} PRIVATE TEST_Z80_WITH_EXECUTE)
endif()
if(${PROJECT_NAME}_FETCH_TEST_FILES)
function(_fetch_files _file_list _location _destination_dir)
file(STRINGS "${_file_list}" _lines)
if(_location MATCHES ".*://.*")
set(_message_action "Downloading")
else()
set(_message_action "Copying")
get_filename_component(_location "${_location}" ABSOLUTE)
set(_location "file://${_location}")
endif()
foreach(_line ${_lines})
string(STRIP "${_line}" _line)
string(SUBSTRING "${_line}" 0 128 _file_hash)
string(SUBSTRING "${_line}" 130 -1 _file_path)
get_filename_component(_file_name "${_file_path}" NAME)
set(_file_url "${_location}/${_file_path}")
string(REPLACE " " "%20" _file_url "${_file_url}")
string(REPLACE "!" "%21" _file_url "${_file_url}")
string(REPLACE "(" "%28" _file_url "${_file_url}")
string(REPLACE ")" "%29" _file_url "${_file_url}")
string(REPLACE "," "%2C" _file_url "${_file_url}")
string(REPLACE "[" "%5B" _file_url "${_file_url}")
string(REPLACE "]" "%5D" _file_url "${_file_url}")
message(STATUS "${_message_action} \"${_file_name}\"")
file( DOWNLOAD
"${_file_url}"
"${_destination_dir}/${_file_path}"
EXPECTED_HASH SHA3_512=${_file_hash})
if( NOT "${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_LESS 3.18 AND
(NOT ZLIB_FOUND OR NOT libzip_FOUND)
)
get_filename_component(_file_extension "${_file_name}" LAST_EXT)
if(_file_extension STREQUAL ".gz" OR _file_extension STREQUAL ".zip")
get_filename_component(_subdirectory "${_file_path}" DIRECTORY)
if(_file_extension STREQUAL ".gz")
set(_extract_pattern "*zex*.com")
else()
set(_extract_pattern "*.tap")
endif()
file( ARCHIVE_EXTRACT
INPUT "${_destination_dir}/${_file_path}"
DESTINATION "${_destination_dir}/${_subdirectory}"
PATTERNS "${_extract_pattern}"
VERBOSE)
endif()
endif()
endforeach()
endfunction()
_fetch_files(
"${CMAKE_CURRENT_SOURCE_DIR}/support/firmware.sha3-512"
"${${PROJECT_NAME}_DEPOT_LOCATION}/firmware"
"${CMAKE_CURRENT_BINARY_DIR}/depot/firmware")
_fetch_files(
"${CMAKE_CURRENT_SOURCE_DIR}/support/software.sha3-512"
"${${PROJECT_NAME}_DEPOT_LOCATION}/software"
"${CMAKE_CURRENT_BINARY_DIR}/depot/software")
if( "${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_LESS 3.18 AND
(NOT ZLIB_FOUND OR NOT libzip_FOUND)
)
message(WARNING
"libzip or zlib were not found on your system, which will cause the "
"test-Z80 tool to be built without archive extraction support. When "
"this happens, the build script extracts those test files that are "
"compressed so that test-Z80 can use them later, but this has failed "
"because the version of CMake you are using is too old and does not "
"support archive extraction.\n"
"To fix this, extract all files with `.tar.gz` or `.zip` extension "
"located in "
"`${CMAKE_CURRENT_BINARY_DIR}/depot/software/**/`")
endif()
endif()
if(${PROJECT_NAME}_FETCH_TEST_FILES OR ${PROJECT_NAME}_DEPOT_LOCATION MATCHES ".*://.*")
set(_depot_dir "${CMAKE_CURRENT_BINARY_DIR}/depot")
else()
set(_depot_dir "${${PROJECT_NAME}_DEPOT_LOCATION}")
endif()
add_test(
NAME test-${PROJECT_NAME}
COMMAND test-${PROJECT_NAME}
--path "${_depot_dir}/firmware"
--path "${_depot_dir}/software/POSIX"
--path "${_depot_dir}/software/ZX Spectrum"
--all
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
unset(_depot_dir)
endif()
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
message("${PROJECT_NAME} END")
endif()
# CMakeLists.txt EOF