-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMakeLists.txt
376 lines (318 loc) · 14.1 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
# To build this project for Raspberry Pi 1, run the following commands in the console:
#
# mkdir cmake-build-raspi
# cd cmake-build-raspi
# cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain-arm-none-eabi.cmake ..
#
# NOTE: by default, CMake generates standard UNIX makefiles. To generate an XCode project on MacOS, invoke CMake
# from the build directory as shown below:
#
# cmake -G Xcode ..
#
cmake_minimum_required(VERSION 3.16.3)
project(zx-raspberry-project LANGUAGES C CXX)
include (CTest)
enable_testing ()
# Do NOT generate verbose makefiles
# [Using CMake with GNU Make: How can I see the exact commands?](https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands)
set(MAKEFLAGS ADD --no-print-directory)
set(CMAKE_RULE_MESSAGES OFF)
set(CMAKE_VERBOSE_MAKEFILE OFF)
# Require out-of-source builds
file(TO_CMAKE_PATH "${CMAKE_BINARY_DIR}/CMakeLists.txt" LOC_PATH)
if (EXISTS "${LOC_PATH}")
message(FATAL_ERROR "You cannot build in a source directory (or any directory with a CMakeLists.txt file). Please make a build subdirectory. Feel free to remove CMakeCache.txt and CMakeFiles.")
endif ()
#set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
# FIXME: these do not appear to work
set(CMAKE_ASM_OUTPUT_EXTENSION .o)
set(CMAKE_C_OUTPUT_EXTENSION .o)
set(CMAKE_CXX_OUTPUT_EXTENSION .o)
# Replace the file extension (rather than append .obj to it) when generating object files
set(CMAKE_ASM_OUTPUT_EXTENSION_REPLACE 1)
set(CMAKE_C_OUTPUT_EXTENSION_REPLACE 1)
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)
set(AARCH 32)
set(RASPPI 4)
set(PREFIX32 arm-none-eabi-)
set(PREFIX64 aarch64-none-elf-)
# set this to "softfp" if you want to link specific libraries
set(FLOAT_ABI hard)
set(CPP ${CMAKE_CXX_COMPILER})
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
if (${AARCH} MATCHES 32)
message(NOTICE "[INFO] Architecture: 32 bits")
if (${RASPPI} MATCHES 1)
message(NOTICE "[INFO] Device: Raspberry Pi 1 (32 bits)")
set(ARCH "-DAARCH=32 -mcpu=arm1176jzf-s -marm -mfpu=vfp -mfloat-abi=${FLOAT_ABI}")
set(TARGET kernel)
elseif (${RASPPI} MATCHES 2)
message(NOTICE "[INFO] Device: Raspberry Pi 2 (32 bits)")
set(ARCH "-DAARCH=32 -mcpu=cortex-a7 -marm -mfpu=neon-vfpv4 -mfloat-abi=${FLOAT_ABI}")
set(TARGET kernel7)
elseif (${RASPPI} MATCHES 3)
message(NOTICE "[INFO] Device: Raspberry Pi 3 (32 bits)")
set(ARCH "-DAARCH=32 -mcpu=cortex-a53 -marm -mfpu=neon-fp-armv8 -mfloat-abi=${FLOAT_ABI}")
set(TARGET kernel8-32)
elseif (${RASPPI} MATCHES 4)
message(NOTICE "[INFO] Device: Raspberry Pi 4 (32 bits)")
set(ARCH "-DAARCH=32 -mcpu=cortex-a72 -marm -mfpu=neon-fp-armv8 -mfloat-abi=${FLOAT_ABI}")
set(TARGET kernel7l)
else ()
message(FATAL_ERROR "error RASPPI must be set to 1, 2, 3 or 4)")
endif ()
set(PREFIX ${PREFIX32})
set(LOADADDR 0x8000)
elseif (${AARCH} MATCHES 64)
message(NOTICE "[INFO] Architecture: 64 bits")
if (${RASPPI} MATCHES 3)
message(NOTICE "[INFO] Device: Raspberry Pi 3 (64 bits)")
set(ARCH "-DAARCH=64 -mcpu=cortex-a53 -mlittle-endian -mcmodel=small")
set(TARGET kernel8)
elseif (${RASPPI} MATCHES 4)
message(NOTICE "[INFO] Device: Raspberry Pi 4 (64 bits)")
set(ARCH "-DAARCH=64 -mcpu=cortex-a72 -mlittle-endian -mcmodel=small")
set(TARGET kernel8-rpi4)
else ()
message(FATAL_ERROR "error RASPPI must be set to 3 or 4")
endif ()
set(PREFIX ${PREFIX64})
set(LOADADDR 0x80000)
else ()
message(FATAL_ERROR "error AARCH must be set to 32 or 64)")
endif ()
# Select the version of the Raspberry Pi board we are building for. This is required because circle uses aliases for
# some classes and the definition of the variable RASPPI is required at build time. Ideally this would be baked into
# Circle so we wouldn't have to do this.
add_definitions(-DRASPPI=${RASPPI})
# For newer versions of CMake
#add_compile_definitions(RASPPI=${RASPPI})
endif ()
# For the bare metal Raspberry Pi system we can only build the static library
if (${CMAKE_SYSTEM_NAME} MATCHES "Generic")
set(Z80CPP_STATIC_ONLY true)
endif ()
# https://stackoverflow.com/questions/5096881/does-set-target-properties-in-cmake-override-cmake-cxx-flags
# https://gitlab.kitware.com/cmake/cmake/issues/17991
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CFLAGS_FOR_TARGET} -O0 -Wall -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CFLAGS_FOR_TARGET} -O0 -Wall -Wextra")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CFLAGS_FOR_TARGET} -O0 -Wall -Wextra -pedantic")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CFLAGS_FOR_TARGET} -O0 -Wall -Wextra -pedantic")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CFLAGS_FOR_TARGET} -O0 -Wall -Wextra -pedantic -Werror")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CFLAGS_FOR_TARGET} -O0 -Wall -Wextra -pedantic -Werror")
#set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS ${BUILD_FLAGS})
# Set the rpath policy and C++ version for MacOS builds
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# set the RPATH policy
set(CMAKE_MACOSX_RPATH 1)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
endif()
set (API_REVISION 0)
set (VERSION_MAJOR 0)
set (VERSION_MINOR 1)
set (RELEASE_TYPE "")
set (VERSION_STR "${API_REVISION}.${VERSION_MAJOR}.${VERSION_MINOR}${RELEASE_TYPE}")
if (CMAKE_COMPILER_IS_GNUCXX)
set (CMAKE_CXX_FLAGS "-Wall -O0 -std=c++17 ${CFLAGS_FOR_TARGET} -Wall -Wextra")
# set (CMAKE_CXX_FLAGS "-Wall -O0 -std=c++14 ${CFLAGS_FOR_TARGET} -Wall -Wextra -pedantic")
# set (CMAKE_CXX_FLAGS "-Wall -O0 -std=c++14 ${CFLAGS_FOR_TARGET} -Wall -Wextra -pedantic -Werror")
# set (CMAKE_CXX_FLAGS "-Wall -O3 -std=c++14 ${CFLAGS_FOR_TARGET}")
endif ()
#include_directories(BEFORE . include)
#
#add_subdirectory(examples)
#set (z80cpp_sources src/z80.cpp include/z80.h include/z80operations.h )
#add_library (z80cpp-static STATIC ${z80cpp_sources})
#set_target_properties (z80cpp-static PROPERTIES OUTPUT_NAME z80cpp)
#if (NOT DEFINED Z80CPP_STATIC_ONLY)
# add_library (z80cpp SHARED ${z80cpp_sources})
# # Affects Win32 only: avoid dynamic/static *.lib files naming conflict
# set_target_properties (z80cpp-static PROPERTIES PREFIX "lib")
#endif ()
#
#if (NOT DEFINED Z80CPP_STATIC_ONLY)
# set_target_properties(z80cpp
# PROPERTIES VERSION ${VERSION_STR} SOVERSION ${API_REVISION}
# )
#endif ()
#
#set_target_properties(z80cpp-static
# PROPERTIES VERSION ${VERSION_STR} SOVERSION ${API_REVISION}
#)
#
#if ("${CMAKE_CPP_IMPLICIT_LINK_DIRECTORIES}" MATCHES "lib64")
# set (LIB_DIR "lib64")
#else ()
# set (LIB_DIR "lib")
#endif ()
#if (NOT DEFINED Z80CPP_STATIC_ONLY)
# install (TARGETS z80cpp LIBRARY DESTINATION ${LIB_DIR} ARCHIVE DESTINATION ${LIB_DIR})
#endif ()
set(INCLUDE_PREFIX "${CMAKE_SOURCE_DIR}/lib")
set(DOCTEST_HOME "${INCLUDE_PREFIX}/doctest/doctest")
# if the target is bare metal ARM (e.g. Raspberry Pi) ...
if (${CMAKE_SYSTEM_NAME} MATCHES "Generic")
# Stop CMake from linking against 'arm-none-eabi/lib/libstdc++.a'
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
set(NEWLIB_ARCH "arm-none-circle")
# For 64 bit support replace with this architecture prefix
# set( NEWLIB_ARCH "aarch64-none-circle" )
set(NEWLIBDIR "${INCLUDE_PREFIX}/circle-stdlib/install/${NEWLIB_ARCH}")
set(CIRCLEHOME "${INCLUDE_PREFIX}/circle-stdlib/libs/circle")
include_directories(
BEFORE
${NEWLIBDIR}/include
${CIRCLEHOME}/include
${DOCTEST_HOME}
)
# Locate the standard C++ library
execute_process(
COMMAND bash "-c" "${CPP} ${ARCH} -print-file-name=libstdc++.a"
OUTPUT_VARIABLE LIBSTDCPP
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (LIBSTDCPP)
message(NOTICE "[INFO] Adding the C++ standard library: '${LIBSTDCPP}'")
list(APPEND EXTRA_LIBS ${LIBSTDCPP})
else()
message(FATAL_ERROR "Unable to find the C++ standard library: 'libstdc++.a'")
endif ()
# Locate the GCC library
execute_process(
COMMAND bash "-c" "${CPP} ${ARCH} -print-file-name=libgcc.a"
OUTPUT_VARIABLE LIBGCC
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (LIBGCC)
message(NOTICE "[INFO] Adding GCC standard library: '${LIBGCC}'")
list(APPEND EXTRA_LIBS ${LIBGCC})
else()
message(FATAL_ERROR "Unable to find GCC standard library: 'libgcc.a'")
endif ()
# WARNING: The order of the libraries in this statement MATTERS and the linker may not be able to resolve some
# symbols if the order is changed. DO NOT change the order of the libraries in this statement unless you know
# what you are doing.
set(
LIBS
"${CIRCLEHOME}/addon/SDCard/libsdcard.a"
# "${CIRCLEHOME}/lib/bt/libbluetooth.a"
"${CIRCLEHOME}/lib/fs/fat/libfatfs.a"
"${CIRCLEHOME}/lib/fs/libfs.a"
"${CIRCLEHOME}/lib/input/libinput.a"
"${CIRCLEHOME}/lib/libcircle.a"
"${CIRCLEHOME}/lib/net/libnet.a"
"${CIRCLEHOME}/lib/sched/libsched.a"
"${CIRCLEHOME}/lib/usb/libusb.a"
"${NEWLIBDIR}/lib/libcirclenewlib.a"
"${NEWLIBDIR}/lib/libc.a"
"${NEWLIBDIR}/lib/libg.a"
"${NEWLIBDIR}/lib/libm.a"
"${NEWLIBDIR}/lib/libnosys.a"
${EXTRA_LIBS}
)
else (${CMAKE_SYSTEM_NAME} MATCHES "Generic")
# Add the Circle compatibility library to the include search path
include_directories(
BEFORE SYSTEM
src/compatibility
${DOCTEST_HOME})
endif (${CMAKE_SYSTEM_NAME} MATCHES "Generic")
# List include directories for debugging
# https://stackoverflow.com/questions/6902149/listing-include-directories-in-cmake
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach (dir ${dirs})
message(STATUS "Include directory: '${dir}'")
endforeach ()
# Enable running the test simulator as part the `make test` target
#set( TEST_SOURCES
# example/z80sim.cpp
# example/z80sim.h
#)
#add_executable( z80sim ${TEST_SOURCES} )
#target_link_libraries( z80sim z80cpp-static ${CIRCLE_LIBS} )
#
#if( ${CMAKE_SYSTEM_NAME} MATCHES "Generic" )
# # Stop CMake from linking against 'arm-none-eabi/lib/libstdc++.a'
# set_target_properties( z80sim PROPERTIES LINKER_LANGUAGE CXX )
#endif()
#
#configure_file( example/zexall.bin zexall.bin COPYONLY )
#
#enable_testing( )
#add_test( NAME z80sim COMMAND z80sim )
#install( TARGETS z80cpp-static LIBRARY DESTINATION ${LIB_DIR} ARCHIVE DESTINATION ${LIB_DIR} )
#install( DIRECTORY include/ DESTINATION include/z80cpp PATTERN "*.h" )
function(generate_kernel_image PROJECT_NAME OUTPUT_NAME)
# "_WE" in variable names is used to indicate the name without extension
#message(STATUS "generate_kernel_image arguments: ${PROJECT_NAME}, followed by ${ARGN}")
get_filename_component(OUTPUT_NAME_WE ${OUTPUT_NAME} NAME_WE)
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND ${CROSS_COMPILE}objdump -d ${PROJECT_NAME} | ${CROSS_COMPILE}c++filt > ${OUTPUT_NAME_WE}.lst
COMMENT " DUMP ${OUTPUT_NAME_WE}.lst"
VERBATIM
)
# Strip the debugging information from the resulting ELF application file
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND ${CROSS_COMPILE}strip ${OUTPUT_NAME}
COMMENT " STRIP ${OUTPUT_NAME}"
VERBATIM
)
# Copy and translate the application file into a binary kernel image
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND ${CROSS_COMPILE}objcopy ${OUTPUT_NAME} -O binary ${OUTPUT_NAME_WE}.img
COMMENT " COPY ${OUTPUT_NAME_WE}.img"
VERBATIM
)
# Print the byte count for the file
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND wc -c < ${OUTPUT_NAME_WE}.img
COMMENT " WC ${OUTPUT_NAME_WE}.img => "
VERBATIM
)
# Compress the kernel image
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND gzip -c ${OUTPUT_NAME_WE}.img > ${OUTPUT_NAME_WE}.img.gz
COMMENT " GZIP ${OUTPUT_NAME_WE}.img => ${OUTPUT_NAME_WE}.img.gz"
VERBATIM
)
# Compress the ELF image
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND gzip -c ${PROJECT_NAME} > ${PROJECT_NAME}.gz
COMMENT " GZIP ${PROJECT_NAME} => ${PROJECT_NAME}.gz"
VERBATIM
)
# Wrap the uncompressed kernel image in an U-Boot legacy header image file so that it can be sent over to U-Boot via
# TFTP e.g. `tftp ${kernel_addr_r} ${bootfile}` and run with bootm e.g. `bootm ${kernel_addr_r} - ${fdt_addr}`.
add_custom_command(
TARGET ${PROJECT_NAME}
# Run after the kernel has been built
POST_BUILD
COMMAND mkimage -n "ZX Raspberry ${VERSION_STR}" -A arm -O linux -T kernel -C none -a ${LOAD_ADDR} -e ${ENTRY_POINT} -d ${OUTPUT_NAME_WE}.img ${OUTPUT_NAME_WE}.uimg && mv ${OUTPUT_NAME_WE}.uimg "${CMAKE_BINARY_DIR}/"
# NOTE: to send a compressed kernel image we need to pass the `-C gzip -d ${OUTPUT_NAME_WE}.img.gz` arguments to mkimage
# COMMAND mkimage -n "ZX Raspberry ${VERSION_STR}" -A arm -O linux -T kernel -C gzip -a ${LOAD_ADDR} -e ${ENTRY_POINT} -d ${OUTPUT_NAME_WE}.img.gz ${OUTPUT_NAME_WE}.uimg && mv ${OUTPUT_NAME_WE}.uimg "${CMAKE_BINARY_DIR}/"
COMMENT " UIMG ${OUTPUT_NAME_WE}.img => ${OUTPUT_NAME_WE}.uimg"
VERBATIM
)
endfunction()
add_subdirectory( src )