Skip to content

Commit 0e8459b

Browse files
authored
Merge pull request #624 from lifting-bits/will/cmake-fixes
CMake fixes
2 parents c91a430 + ab9bc05 commit 0e8459b

File tree

10 files changed

+136
-164
lines changed

10 files changed

+136
-164
lines changed

CMakeLists.txt

Lines changed: 31 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,24 @@ message(STATUS "Compiler ID is ${CMAKE_C_COMPILER_ID}")
3636
# libraries
3737
#
3838

39+
# Z3
40+
find_package(Z3 CONFIG REQUIRED)
41+
3942
# LLVM
4043
find_package(LLVM CONFIG REQUIRED)
44+
llvm_map_components_to_libnames(llvm_libs
45+
support core irreader
46+
bitreader bitwriter
47+
passes asmprinter
48+
aarch64codegen aarch64asmparser
49+
armcodegen armasmparser
50+
interpreter mcjit
51+
nvptxdesc
52+
x86codegen x86asmparser
53+
sparccodegen sparcasmparser
54+
webassemblydesc)
55+
message(STATUS "LLVM Libraries: ${llvm_libs}")
56+
4157
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
4258
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
4359

@@ -60,66 +76,16 @@ set(REMILL_BUILD_SEMANTICS_DIR_SPARC64 "${CMAKE_CURRENT_BINARY_DIR}/lib/Arch/SPA
6076
set(REMILL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
6177
set(REMILL_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")
6278

63-
add_library(thirdparty_llvm INTERFACE)
64-
target_include_directories(thirdparty_llvm SYSTEM INTERFACE
65-
$<BUILD_INTERFACE:${LLVM_INCLUDE_DIRS}>
66-
)
67-
target_compile_definitions(thirdparty_llvm INTERFACE
68-
${LLVM_DEFINITIONS}
69-
)
7079
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/BCCompiler.cmake")
7180

72-
# Go find only the static libraries of LLVM, and link against those.
73-
foreach(LLVM_LIB IN LISTS LLVM_AVAILABLE_LIBS)
74-
get_target_property(LLVM_LIB_TYPE ${LLVM_LIB} TYPE)
75-
76-
if(LLVM_LIB_TYPE STREQUAL "STATIC_LIBRARY")
77-
list(APPEND LLVM_LIBRARIES "${LLVM_LIB}")
78-
endif()
79-
endforeach()
80-
81-
# These are out-of-order in `LLVM_AVAILABLE_LIBS` and should always be last.
82-
list(REMOVE_ITEM LLVM_LIBRARIES LLVMMC LLVMCore LLVMSupport)
83-
list(APPEND LLVM_LIBRARIES LLVMMC LLVMCore LLVMSupport)
84-
85-
target_link_libraries(thirdparty_llvm INTERFACE
86-
${LLVM_LIBRARIES}
87-
)
88-
89-
# Microsoft Z3 with LLVM. Not exactly used in remill, but LLVM doesn't link
90-
# against it correctly
91-
# NOTE: If changing this, also replicate in remillConfig file
92-
if(LLVM_WITH_Z3)
93-
find_package(Z3 CONFIG REQUIRED 4.7.1)
94-
get_target_property(LLVMSupport_LIBS LLVMSupport INTERFACE_LINK_LIBRARIES)
95-
list(REMOVE_ITEM LLVMSupport_LIBS Z3)
96-
list(APPEND LLVMSupport_LIBS z3::libz3)
97-
set_target_properties(LLVMSupport PROPERTIES
98-
INTERFACE_LINK_LIBRARIES "${LLVMSupport_LIBS}")
99-
endif()
100-
101-
message(STATUS "LLVM Libraries: ${LLVM_LIBRARIES}")
102-
10381
# Intel XED
10482
find_package(XED CONFIG REQUIRED)
105-
add_library(thirdparty_xed INTERFACE)
106-
target_link_libraries(thirdparty_xed INTERFACE
107-
XED::XED
108-
)
10983

11084
# Google glog module
11185
find_package(glog CONFIG REQUIRED)
112-
add_library(thirdparty_glog INTERFACE)
113-
target_link_libraries(thirdparty_glog INTERFACE
114-
glog::glog
115-
)
11686

11787
# Google gflags
11888
find_package(gflags CONFIG REQUIRED)
119-
add_library(thirdparty_gflags INTERFACE)
120-
target_link_libraries(thirdparty_gflags INTERFACE
121-
gflags
122-
)
12389

12490
set(sleigh_ENABLE_TESTS OFF)
12591
set(sleigh_ADDITIONAL_PATCHES "${CMAKE_CURRENT_SOURCE_DIR}/patches/sleigh/x86-ia.patch;${CMAKE_CURRENT_SOURCE_DIR}/patches/sleigh/arm-thumb.patch" CACHE STRING "" FORCE)
@@ -134,22 +100,6 @@ set(sleigh_BUILD_SUPPORT ON CACHE BOOL "" FORCE)
134100
set(sleigh_BUILD_SLEIGHSPECS ON CACHE BOOL "" FORCE)
135101
FetchContent_MakeAvailable(sleigh)
136102

137-
add_library(thirdparty_sleigh INTERFACE)
138-
target_link_libraries(thirdparty_sleigh INTERFACE
139-
sleigh::sla
140-
sleigh::decomp
141-
sleigh::support
142-
)
143-
144-
# Windows SDK
145-
add_library(thirdparty_win32 INTERFACE)
146-
147-
if(DEFINED WIN32)
148-
target_link_libraries(thirdparty_win32 INTERFACE
149-
"Kernel32.lib"
150-
)
151-
endif()
152-
153103
# For Linux builds, group LLVM libraries into a single group
154104
# that avoids frustrating library ordering issues.
155105
if(UNIX AND NOT APPLE)
@@ -255,9 +205,14 @@ target_compile_definitions(remill_settings INTERFACE
255205
"REMILL_BUILD_SEMANTICS_DIR_SPARC64=\"${REMILL_BUILD_SEMANTICS_DIR_SPARC64}\""
256206
)
257207

258-
set(THIRDPARTY_LIBRARY_LIST thirdparty_llvm thirdparty_xed thirdparty_glog thirdparty_gflags thirdparty_sleigh)
259208
target_link_libraries(remill_settings INTERFACE
260-
${THIRDPARTY_LIBRARY_LIST}
209+
${llvm_libs}
210+
XED::XED
211+
glog::glog
212+
gflags::gflags
213+
sleigh::sla
214+
sleigh::decomp
215+
sleigh::support
261216
)
262217

263218
add_subdirectory(lib/Arch)
@@ -268,7 +223,13 @@ add_subdirectory(lib/Version)
268223
add_library(remill INTERFACE)
269224
target_link_libraries(remill INTERFACE
270225
${LINKER_START_GROUP}
271-
${THIRDPARTY_LIBRARY_LIST}
226+
${llvm_libs}
227+
XED::XED
228+
glog::glog
229+
gflags::gflags
230+
sleigh::sla
231+
sleigh::decomp
232+
sleigh::support
272233
remill_bc
273234
remill_os
274235
remill_arch
@@ -286,28 +247,6 @@ InstallExternalTarget("ext_clang" "${CLANG_PATH}" "BIN" "${INSTALLED_CLANG_NAME}
286247

287248
InstallExternalTarget("ext_llvmlink" "${LLVMLINK_PATH}" "BIN" "${INSTALLED_LLVMLINK_NAME}")
288249

289-
GetTargetTree(THIRDPARTY_LIBRARIES ${THIRDPARTY_LIBRARY_LIST})
290-
GetPublicIncludeFolders(THIRDPARTY_INCLUDE_DIRECTORIES ${THIRDPARTY_LIBRARIES})
291-
292-
foreach(THIRDPARTY_LIB IN LISTS THIRDPARTY_LIBRARIES)
293-
string(SUBSTRING "${THIRDPARTY_LIB}" 0 1 THIRDPARTY_LIB_PREFIX)
294-
295-
if(TARGET ${THIRDPARTY_LIB})
296-
get_target_property(THIRDPARTY_LIB_TYPE ${THIRDPARTY_LIB} TYPE)
297-
298-
if(THIRDPARTY_LIB_TYPE STREQUAL "STATIC_LIBRARY" OR THIRDPARTY_LIB_TYPE STREQUAL "SHARED_LIBRARY")
299-
list(APPEND THIRDPARTY_LIBRARY_FILES "$${}<TARGET_FILE:${THIRDPARTY_LIB}>")
300-
endif()
301-
elseif("${THIRDPARTY_LIB_PREFIX}" STREQUAL "$${}")
302-
# E.g. $<LINK_ONLY:...>
303-
else()
304-
list(APPEND THIRDPARTY_LIBRARY_FILES "${THIRDPARTY_LIB}")
305-
endif()
306-
endforeach()
307-
308-
list(REMOVE_DUPLICATES THIRDPARTY_LIBRARY_FILES)
309-
310-
#
311250
# additional targets
312251
#
313252
add_custom_target(semantics)
@@ -321,11 +260,10 @@ add_subdirectory(bin)
321260
if(REMILL_ENABLE_INSTALL_TARGET)
322261
install(TARGETS remill EXPORT remillTargets)
323262

324-
install(TARGETS remill_settings ${THIRDPARTY_LIBRARY_LIST}
263+
install(TARGETS remill_settings
325264
EXPORT remillTargets
326265
)
327266

328-
# First do the basic substitutions.
329267
configure_file(
330268
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/remillConfig.cmake.in"
331269
"${CMAKE_CURRENT_BINARY_DIR}/remillConfig.cmake"

bin/differential_tester_x86/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ target_link_libraries(
2424
lift-and-compare
2525
PRIVATE
2626
remill
27-
thirdparty_glog
27+
glog::glog
2828
test-runner
2929
)
3030

3131
set_property(TARGET lift-and-compare PROPERTY ENABLE_EXPORTS ON)
3232
set_property(TARGET lift-and-compare PROPERTY POSITION_INDEPENDENT_CODE ON)
3333
enable_testing()
3434

35-
add_test(NAME "small_diff_test" COMMAND "${Python_EXECUTABLE}" ${REMILL_SOURCE_DIR}/scripts/diff_tester_export_insns/diff_tester_export_insns/ci_runner.py --required_success_rate 1.0 --difftester_bin ${CMAKE_BINARY_DIR}/bin/differential_tester_x86/lift-and-compare --workdir ${CMAKE_BINARY_DIR} ${REMILL_SOURCE_DIR}/bin/differential_tester_x86/data/small_test/ --whitelist_file ${REMILL_SOURCE_DIR}/bin/differential_tester_x86/whitelist.json)
35+
add_test(NAME "small_diff_test" COMMAND "${Python_EXECUTABLE}" ${REMILL_SOURCE_DIR}/scripts/diff_tester_export_insns/diff_tester_export_insns/ci_runner.py --required_success_rate 1.0 --difftester_bin ${CMAKE_BINARY_DIR}/bin/differential_tester_x86/lift-and-compare --workdir ${CMAKE_BINARY_DIR} ${REMILL_SOURCE_DIR}/bin/differential_tester_x86/data/small_test/ --whitelist_file ${REMILL_SOURCE_DIR}/bin/differential_tester_x86/whitelist.json)

cmake/git_watcher.cmake

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# git_watcher.cmake
2-
# https://github.com/andrew-hardin/cmake-git-version-tracking/blob/20c58d5d08bad70550d969b478943c962faa6264/git_watcher.cmake
2+
# https://raw.githubusercontent.com/andrew-hardin/cmake-git-version-tracking/master/git_watcher.cmake
33
#
44
# Released under the MIT License.
55
# https://raw.githubusercontent.com/andrew-hardin/cmake-git-version-tracking/master/LICENSE
@@ -27,6 +27,15 @@
2727
# -- The path to the git executable. It'll automatically be set if the
2828
# user doesn't supply a path.
2929
#
30+
# GIT_FAIL_IF_NONZERO_EXIT (OPTIONAL)
31+
# -- Raise a FATAL_ERROR if any of the git commands return a non-zero
32+
# exit code. This is set to TRUE by default. You can set this to FALSE
33+
# if you'd like the build to continue even if a git command fails.
34+
#
35+
# GIT_IGNORE_UNTRACKED (OPTIONAL)
36+
# -- Ignore the presence of untracked files when detecting if the
37+
# working tree is dirty. This is set to FALSE by default.
38+
#
3039
# DESIGN
3140
# - This script was designed similar to a Python application
3241
# with a Main() function. I wanted to keep it compact to
@@ -57,17 +66,25 @@ macro(CHECK_REQUIRED_VARIABLE var_name)
5766
endmacro()
5867

5968
# Check that an optional variable is set, or, set it to a default value.
60-
macro(CHECK_OPTIONAL_VARIABLE var_name default_value)
69+
macro(CHECK_OPTIONAL_VARIABLE_NOPATH var_name default_value)
6170
if(NOT DEFINED ${var_name})
6271
set(${var_name} ${default_value})
6372
endif()
73+
endmacro()
74+
75+
# Check that an optional variable is set, or, set it to a default value.
76+
# Also converts that path to an abspath.
77+
macro(CHECK_OPTIONAL_VARIABLE var_name default_value)
78+
CHECK_OPTIONAL_VARIABLE_NOPATH(${var_name} ${default_value})
6479
PATH_TO_ABSOLUTE(${var_name})
6580
endmacro()
6681

6782
CHECK_REQUIRED_VARIABLE(PRE_CONFIGURE_FILE)
6883
CHECK_REQUIRED_VARIABLE(POST_CONFIGURE_FILE)
69-
CHECK_OPTIONAL_VARIABLE(GIT_STATE_FILE "${CMAKE_BINARY_DIR}/git-state-hash")
84+
CHECK_OPTIONAL_VARIABLE(GIT_STATE_FILE "${CMAKE_CURRENT_BINARY_DIR}/git-state-hash")
7085
CHECK_OPTIONAL_VARIABLE(GIT_WORKING_DIR "${CMAKE_SOURCE_DIR}")
86+
CHECK_OPTIONAL_VARIABLE_NOPATH(GIT_FAIL_IF_NONZERO_EXIT TRUE)
87+
CHECK_OPTIONAL_VARIABLE_NOPATH(GIT_IGNORE_UNTRACKED FALSE)
7188

7289
# Check the optional git variable.
7390
# If it's not set, we'll try to find it using the CMake packaging system.
@@ -86,7 +103,8 @@ set(_state_variable_names
86103
GIT_COMMIT_DATE_ISO8601
87104
GIT_COMMIT_SUBJECT
88105
GIT_COMMIT_BODY
89-
VERSION_STRING
106+
GIT_DESCRIBE
107+
GIT_BRANCH
90108
# >>>
91109
# 1. Add the name of the additional git variable you're interested in monitoring
92110
# to this list.
@@ -96,17 +114,30 @@ set(_state_variable_names
96114

97115
# Macro: RunGitCommand
98116
# Description: short-hand macro for calling a git function. Outputs are the
99-
# "exit_code" and "output" variables.
117+
# "exit_code" and "output" variables. The "_permit_git_failure"
118+
# variable can locally override the exit code checking- use it
119+
# with caution.
100120
macro(RunGitCommand)
101121
execute_process(COMMAND
102122
"${GIT_EXECUTABLE}" ${ARGV}
103123
WORKING_DIRECTORY "${_working_dir}"
104124
RESULT_VARIABLE exit_code
105125
OUTPUT_VARIABLE output
106-
ERROR_QUIET
126+
ERROR_VARIABLE stderr
107127
OUTPUT_STRIP_TRAILING_WHITESPACE)
108-
if(NOT exit_code EQUAL 0)
128+
if(NOT exit_code EQUAL 0 AND NOT _permit_git_failure)
109129
set(ENV{GIT_RETRIEVED_STATE} "false")
130+
131+
# Issue 26: git info not properly set
132+
#
133+
# Check if we should fail if any of the exit codes are non-zero.
134+
# Most methods have a fall-back default value that's used in case of non-zero
135+
# exit codes. If you're feeling risky, disable this safety check and use
136+
# those default values.
137+
if(GIT_FAIL_IF_NONZERO_EXIT )
138+
string(REPLACE ";" " " args_with_spaces "${ARGV}")
139+
message(FATAL_ERROR "${stderr} (${GIT_EXECUTABLE} ${args_with_spaces})")
140+
endif()
110141
endif()
111142
endmacro()
112143

@@ -123,7 +154,12 @@ function(GetGitState _working_dir)
123154
set(ENV{GIT_RETRIEVED_STATE} "true")
124155

125156
# Get whether or not the working tree is dirty.
126-
RunGitCommand(status --porcelain)
157+
if (GIT_IGNORE_UNTRACKED)
158+
set(untracked_flag "-uno")
159+
else()
160+
set(untracked_flag "-unormal")
161+
endif()
162+
RunGitCommand(status --porcelain ${untracked_flag})
127163
if(NOT exit_code EQUAL 0)
128164
set(ENV{GIT_IS_DIRTY} "false")
129165
else()
@@ -158,6 +194,8 @@ function(GetGitState _working_dir)
158194

159195
RunGitCommand(show -s "--format=%s" ${object})
160196
if(exit_code EQUAL 0)
197+
# Escape \
198+
string(REPLACE "\\" "\\\\" output "${output}")
161199
# Escape quotes
162200
string(REPLACE "\"" "\\\"" output "${output}")
163201
set(ENV{GIT_COMMIT_SUBJECT} "${output}")
@@ -166,6 +204,8 @@ function(GetGitState _working_dir)
166204
RunGitCommand(show -s "--format=%b" ${object})
167205
if(exit_code EQUAL 0)
168206
if(output)
207+
# Escape \
208+
string(REPLACE "\\" "\\\\" output "${output}")
169209
# Escape quotes
170210
string(REPLACE "\"" "\\\"" output "${output}")
171211
# Escape line breaks in the commit message.
@@ -178,9 +218,9 @@ function(GetGitState _working_dir)
178218
# There was no commit body - set the safe string to empty.
179219
set(safe "")
180220
endif()
181-
set(ENV{GIT_COMMIT_BODY} "\"${safe}\"")
221+
set(ENV{GIT_COMMIT_BODY} "${safe}")
182222
else()
183-
set(ENV{GIT_COMMIT_BODY} "\"\"") # empty string.
223+
set(ENV{GIT_COMMIT_BODY} "") # empty string.
184224
endif()
185225

186226
# Get output of git describe
@@ -191,20 +231,23 @@ function(GetGitState _working_dir)
191231
set(ENV{GIT_DESCRIBE} "${output}")
192232
endif()
193233

234+
# Convert HEAD to a symbolic ref. This can fail, in which case we just
235+
# set that variable to HEAD.
236+
set(_permit_git_failure ON)
237+
RunGitCommand(symbolic-ref --short -q ${object})
238+
unset(_permit_git_failure)
239+
if(NOT exit_code EQUAL 0)
240+
set(ENV{GIT_BRANCH} "${object}")
241+
else()
242+
set(ENV{GIT_BRANCH} "${output}")
243+
endif()
244+
194245
# >>>
195246
# 2. Additional git properties can be added here via the
196247
# "execute_process()" command. Be sure to set them in
197248
# the environment using the same variable name you added
198249
# to the "_state_variable_names" list.
199250

200-
if(EXISTS "${GIT_WORKING_DIR}/VERSION")
201-
file(READ "${GIT_WORKING_DIR}/VERSION" version_output_raw)
202-
string(STRIP "${version_output_raw}" version_output)
203-
set(ENV{VERSION_STRING} "${version_output}")
204-
else()
205-
set(ENV{VERSION_STRING} "")
206-
endif()
207-
208251
endfunction()
209252

210253

@@ -296,6 +339,8 @@ function(SetupGitMonitoring)
296339
-DGIT_STATE_FILE=${GIT_STATE_FILE}
297340
-DPRE_CONFIGURE_FILE=${PRE_CONFIGURE_FILE}
298341
-DPOST_CONFIGURE_FILE=${POST_CONFIGURE_FILE}
342+
-DGIT_FAIL_IF_NONZERO_EXIT=${GIT_FAIL_IF_NONZERO_EXIT}
343+
-DGIT_IGNORE_UNTRACKED=${GIT_IGNORE_UNTRACKED}
299344
-P "${CMAKE_CURRENT_LIST_FILE}")
300345
endfunction()
301346

0 commit comments

Comments
 (0)