-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
197 lines (171 loc) · 6.66 KB
/
Copy pathCMakeLists.txt
File metadata and controls
197 lines (171 loc) · 6.66 KB
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
cmake_minimum_required(VERSION 3.27)
project(fp32-ximera-fsw-algorithms)
macro(add_algorithm name)
file(GLOB source_files
"${PROJECT_SOURCE_DIR}/algorithms/${name}/*Algorithm.cpp"
"${PROJECT_SOURCE_DIR}/algorithms/${name}/*Algorithm_c.cpp")
set(target_name "${name}")
add_library(${target_name} OBJECT ${source_files})
target_include_directories(${target_name} PRIVATE ${PROJECT_SOURCE_DIR}/algorithms/${name})
target_include_directories(${target_name} PRIVATE ${PROJECT_SOURCE_DIR})
target_include_directories(${target_name} PRIVATE ${EIGEN3_DIR})
# FswUtilities supplies the "utilities/fsw/..." include prefix (the algorithms/
# dir) as a usage requirement, the same way the xmera module build consumes it.
target_link_libraries(${target_name} PRIVATE FswUtilities)
# The freestanding compile setup is specific to this standalone build path
# (the xmera module build uses hosted Eigen), so it lives here, not on the
# shared FswUtilities target.
# Included via -include to ensure it's included before any other translation unit
target_compile_options(${target_name} PRIVATE
-include ${EIGEN3_DIR}/Eigen/src/Freestanding/all_freestanding.hpp
)
set_target_properties(${target_name} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
target_compile_definitions(${target_name} PRIVATE EIGEN_FREESTANDING=1)
message(STATUS "Added algorithms ${target_name}")
endmacro()
macro(generate_ada_spec algorithm_name ada_specs)
set(ada_spec "${BINDINGS_DIR}/${algorithm_name}Algorithm_c.ads")
add_custom_command(
OUTPUT "${ada_spec}"
WORKING_DIRECTORY "${BINDINGS_DIR}"
COMMAND ${CMAKE_CXX_COMPILER}
"-c"
"-fdump-ada-spec"
"-C" "${PROJECT_SOURCE_DIR}/algorithms/${algorithm_name}/${algorithm_name}Algorithm_c.h"
-I${PROJECT_SOURCE_DIR}
-I${PROJECT_SOURCE_DIR}/algorithms/${algorithm_name}
-I${PROJECT_SOURCE_DIR}/algorithms
-I${EIGEN3_DIR}
"-DEIGEN_FREESTANDING=1"
COMMENT "Generating Ada spec: ${ada_spec}"
VERBATIM
)
list(APPEND ada_specs "${ada_spec}")
endmacro()
# 1) Force C++23, PIC
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# 2) Ensure CMAKE_BUILD_TYPE is defined (default=Release)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Debug or Release" FORCE)
endif()
string(TOUPPER "${CMAKE_BUILD_TYPE}" BUILD_TYPE_UPPER)
message(STATUS "Build type: ${BUILD_TYPE_UPPER}")
# 3) Native Linux flags (only when not cross-compiling)
if(NOT CMAKE_CROSSCOMPILING)
if(BUILD_TYPE_UPPER STREQUAL "DEBUG")
message(STATUS "Configuring native Linux Debug flags")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -g" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} " CACHE STRING "" FORCE)
else()
message(STATUS "Configuring native Linux Release flags")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -DNDEBUG" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} " CACHE STRING "" FORCE)
endif()
endif()
# 4) Eigen include (always /home/user/eigen) and project headers
# Let user override via -DEIGEN3_DIR=... or environment
set(EIGEN3_DIR "$ENV{EIGEN3_DIR}" CACHE PATH "Path to Eigen root (directory containing 'Eigen/')")
# Fallback default if not provided
if(NOT EIGEN3_DIR)
set(EIGEN3_DIR "/home/user/eigen" CACHE PATH "Path to Eigen root" FORCE)
endif()
# Normalize to absolute so generators don’t see it as relative
get_filename_component(EIGEN3_DIR "${EIGEN3_DIR}" ABSOLUTE)
# Sanity check
if(NOT IS_DIRECTORY "${EIGEN3_DIR}/Eigen")
message(FATAL_ERROR "Eigen not found under ${EIGEN3_DIR}")
endif()
# Define the header-only FswUtilities target before add_algorithm() links it.
# It carries only the "utilities/fsw/..." include prefix; Eigen and the
# freestanding flags are added per-algorithm above (this build is freestanding;
# the xmera module build is not, which is why those are not on the target).
add_subdirectory(algorithms/utilities/fsw)
# "rwMotorVoltage" "stepperMotorController"
set(algorithms
"attTrackingError"
"averageMimuData"
"bodyRateMiscompare"
"celestialTwoBodyPoint"
"convertStPlatformToBody"
"cssComm"
"dvGuidance"
"ephemeridesRecenter"
"forceTorqueThrForceMapping"
"hillPoint"
"inertial3D"
"inertialUKF"
"mimuMajorityVote"
"mrpFeedback"
"mrpRotation"
"navAggregate"
"oeStateEphem"
"rateControl"
"rwMotorVoltage"
"solarArrayReference"
"stepperMotorController"
"sunlineEphem"
"sunlineSRuKF"
"sunTrackError"
"sunSafePoint"
"sunSearch"
"thrFiringRemainder"
"thrFiringSchmitt"
)
foreach(algorithm_name IN LISTS algorithms)
add_algorithm(${algorithm_name})
endforeach()
# 7) Build the static library (libgncAlgorithms.a)
set(combined_objects)
foreach(lib ${algorithms})
list(APPEND combined_objects $<TARGET_OBJECTS:${lib}>)
endforeach()
add_library(gncAlgorithms STATIC ${combined_objects})
# Place the archive in build/lib/
set_target_properties(gncAlgorithms PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib
)
target_link_libraries(gncAlgorithms PUBLIC FswUtilities)
if(GENERATE_ADA_BINDINGS)
# 8) Generate Ada bindings (always, for both Linux and RISC-V)
set(BINDINGS_DIR "${PROJECT_BINARY_DIR}/bindings")
file(MAKE_DIRECTORY "${BINDINGS_DIR}")
set(ada_specs "")
foreach(module_name IN LISTS algorithms)
generate_ada_spec(${module_name} ada_specs)
endforeach()
add_custom_target(generate_ada_bindings ALL
DEPENDS ${ada_specs}
)
add_dependencies(gncAlgorithms generate_ada_bindings)
endif()
# 9) Usage summary:
# Native Linux (Debug):
# mkdir build_linux_debug && cd build_linux_debug
# cmake -DCMAKE_BUILD_TYPE=Debug ..
# make
#
# Native Linux (Release):
# mkdir build_linux_release && cd build_linux_release
# cmake -DCMAKE_BUILD_TYPE=Release ..
# make
#
# RISC-V 32 (Debug, Ada bindings):
# mkdir build_riscv_debug && cd build_riscv_debug
# cmake -DCMAKE_BUILD_TYPE=Debug \
# -DCMAKE_TOOLCHAIN_FILE=../riscv32-toolchain.cmake \
# ..
# make
#
# RISC-V 32 (Release, Ada bindings):
# mkdir build_riscv_release && cd build_riscv_release
# cmake -DCMAKE_BUILD_TYPE=Release \
# -DCMAKE_TOOLCHAIN_FILE=../riscv32-toolchain.cmake \
# ..
# make
#
# The generated Ada specs are in <build_dir>/bindings/*.ads
# The static archive is in <build_dir>/lib/libgncAlgorithms.a