11#
2- # Copyright (c) 2020-present, Trail of Bits, Inc.
3- # All rights reserved.
2+ # Copyright (c) 2025-present, Trail of Bits, Inc.
43#
54# This source code is licensed in accordance with the terms specified in
65# the LICENSE file found in the root directory of this source tree.
76#
87
9- cmake_minimum_required (VERSION 3.21)
8+ set (src_dir "${CMAKE_CURRENT_LIST_DIR} /src" )
9+ set (build_dir "${CMAKE_CURRENT_BINARY_DIR} /build" )
1010
11- include ("cmake/options.cmake" )
11+ # Fetch the submodule if we don't yet have it.
12+ if (NOT EXISTS "${src_dir} /CMakeLists.txt" )
13+ execute_process (
14+ COMMAND "${GIT_EXECUTABLE} " submodule update --init vendor/rellic/src
15+ WORKING_DIRECTORY "${PROJECT_SOURCE_DIR} " )
1216
13- project (rellic)
14- set (CMAKE_CXX_VISIBILITY_PRESET hidden)
15- set (CMAKE_C_VISIBILITY_PRESET hidden)
16- set (VISIBILITY_INLINES_HIDDEN YES )
17- include ("cmake/settings.cmake" )
18- include (GNUInstallDirs)
19-
20- if (RELLIC_ENABLE_INSTALL)
21- include ("cmake/packaging.cmake" )
22- endif (RELLIC_ENABLE_INSTALL)
23-
24- enable_language (C CXX ASM)
25-
26- set (RELLIC_SOURCE_DIR "${PROJECT_SOURCE_DIR} " )
27-
28- add_library ("${PROJECT_NAME} _cxx_settings" INTERFACE )
29- target_compile_features ("${PROJECT_NAME} _cxx_settings"
30- INTERFACE
31- cxx_std_17
32- )
33-
34- # warnings and compiler settings
35- if (NOT DEFINED WIN32 )
36- target_compile_options ("${PROJECT_NAME} _cxx_settings"
37- INTERFACE
38- -Werror
39- # -Wconversion
40- -pedantic
41- -Wno-unreachable-code-return
42- )
43- endif (NOT DEFINED WIN32 )
44-
45- #
46- # libraries
47- #
48-
49- find_package (gflags CONFIG REQUIRED)
50- find_package (glog CONFIG REQUIRED)
51- find_package (Z3 4.8 CONFIG REQUIRED)
52- find_package (doctest CONFIG REQUIRED)
53- find_package (LLVM 20 CONFIG REQUIRED)
54- message (STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR} " )
55- llvm_map_components_to_libnames(llvm_libs support core irreader bitreader bitwriter)
56-
57- find_package (MLIR 20 CONFIG REQUIRED)
58- message (STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR} " )
59-
60- find_package (Clang 20 CONFIG REQUIRED)
61- message (STATUS "Using ClangConfig.cmake in: ${Clang_DIR} " )
62-
63-
64- #
65- # helper macro to set target properties
66- #
67-
68- if (RELLIC_ENABLE_INSTALL)
69- export (PACKAGE "${PROJECT_NAME} " )
70-
71- set (cmake_install_dir "lib/cmake/${PROJECT_NAME} " )
72-
73- include (CMakePackageConfigHelpers)
74- configure_package_config_file("${PROJECT_NAME} Config.cmake.in"
75- "${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake"
76- INSTALL_DESTINATION "${cmake_install_dir} "
77- )
78-
79- install (
80- FILES
81- "${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake"
82- DESTINATION "${cmake_install_dir} "
83- )
84- install (EXPORT "${PROJECT_NAME} Targets"
85- DESTINATION "${cmake_install_dir} "
86- NAMESPACE "${PROJECT_NAME} ::"
87- )
88- install (
89- TARGETS
90- "${PROJECT_NAME} _cxx_settings"
91- EXPORT
92- "${PROJECT_NAME} Targets"
93- )
94- endif (RELLIC_ENABLE_INSTALL)
95-
96- #
97- # rellic libraries
98- #
99-
100- add_subdirectory (lib)
101-
102- #
103- # rellic executables
104- #
105-
106- add_subdirectory (tools)
107-
108- #
109- # tests
110- #
111-
112-
113- if (RELLIC_ENABLE_TESTING)
114- enable_testing ()
115-
116- add_subdirectory (unittests)
117-
118- get_target_property (CLANG_PATH clang LOCATION )
119- message (STATUS "Clang path for tests: \" ${CLANG_PATH} \" " )
120-
121- find_package (Python3 COMPONENTS Interpreter REQUIRED)
122- message (STATUS "Python path for tests: \" ${Python3_EXECUTABLE} \" " )
123-
124- if (DEFINED CMAKE_OSX_SYSROOT)
125- set (RELLIC_TEST_ARGS "--cflags=-isysroot" "--cflags=${CMAKE_OSX_SYSROOT} " )
126- else ()
127- set (RELLIC_TEST_ARGS "" )
128- endif ()
129-
130- # Tests that survive a complete roundtrip
131- add_test (NAME test_roundtrip_rebuild
132- COMMAND "${Python3_EXECUTABLE} " scripts/roundtrip.py $<TARGET_FILE:${RELLIC_DECOMP} > tests/tools/decomp/ "${CLANG_PATH} " --timeout 30 ${RELLIC_TEST_ARGS}
133- WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
134- )
17+ execute_process (
18+ COMMAND "${GIT_EXECUTABLE} " submodule set-branch --branch llvm20 vendor/rellic/src
19+ WORKING_DIRECTORY "${PROJECT_SOURCE_DIR} " )
20+ endif ()
13521
136- # Tests that may not roundtrip yet, but should emit C
137- add_test (NAME test_roundtrip_translate_only
138- COMMAND "${Python3_EXECUTABLE} " scripts/roundtrip.py --translate-only $<TARGET_FILE:${RELLIC_DECOMP} > tests/tools/decomp/failing-rebuild/ "${CLANG_PATH} " --timeout 30 ${RELLIC_TEST_ARGS}
139- WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
22+ # Only fetch doctest if testing is enabled
23+ if (RELLIC_ENABLE_TESTING)
24+ include (FetchContent)
25+ FetchContent_Declare(
26+ doctest
27+ GIT_REPOSITORY https://github.com/doctest/doctest.git
28+ GIT_TAG v2.4.11
14029 )
30+ FetchContent_MakeAvailable(doctest)
31+ endif ()
14132
142- # Tests that can be decompiled
143- add_test (NAME test_decompile
144- COMMAND "${Python3_EXECUTABLE} " scripts/decompile.py $<TARGET_FILE:${RELLIC_DECOMP} > tests/tools/decomp/ --timeout 30
145- WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
146- )
33+ set (REMILL_PREFIX_PATH ${CMAKE_PREFIX_PATH} )
14734
148- add_test (NAME test_headergen
149- COMMAND "${Python3_EXECUTABLE} " scripts/test -headergen.py $<TARGET_FILE:${RELLIC_HEADERGEN} > tests/tools/headergen/ "${CLANG_PATH} " ${RELLIC_TEST_ARGS}
150- WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
151- )
35+ if (PE_USE_VENDORED_CLANG)
36+ set (REMILL_PREFIX_PATH ${PE_VENDOR_INSTALL_DIR} )
15237endif ()
38+
39+ execute_process (COMMAND ${CMAKE_COMMAND} -E make_directory "${build_dir} " )
40+
41+ execute_process (COMMAND ${CMAKE_COMMAND}
42+ -G "${CMAKE_GENERATOR} "
43+ -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
44+ "-DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} "
45+ "-DCMAKE_INSTALL_PREFIX=${PE_VENDOR_INSTALL_DIR} "
46+ "-DCMAKE_PREFIX_PATH=${REMILL_PREFIX_PATH} "
47+ "-DCMAKE_INSTALL_RPATH=${CMAKE_INSTALL_RPATH} "
48+ "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} "
49+ "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} "
50+ "-Dglog_DIR=${PE_VENDOR_INSTALL_DIR} /lib/cmake/glog"
51+ -DRELLIC_ENABLE_INSTALL=ON
52+ -DRELLIC_ENABLE_TESTING=OFF
53+ "${src_dir} "
54+ WORKING_DIRECTORY "${build_dir} " )
55+
56+ # (c) Build just mycomp in the nested build. Don't specify a --config
57+ # because we cannot know what config the developer will be using
58+ # at this point. For non-multi-config generators, we've already
59+ # specified CMAKE_BUILD_TYPE above in (b).
60+ execute_process (COMMAND ${CMAKE_COMMAND} --build . --target install
61+ WORKING_DIRECTORY "${build_dir} " )
62+
63+ if (DEFINED ENV{CI})
64+ execute_process (COMMAND ${CMAKE_COMMAND} -E remove_directory "${build_dir} " )
65+ endif ()
0 commit comments