Skip to content

Commit

Permalink
cmake: fix SetClangRTLibDir [TO SQUASH]
Browse files Browse the repository at this point in the history
  • Loading branch information
ligurio committed Dec 29, 2023
1 parent 64e6d19 commit e788079
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions cmake/SetClangRTLibDir.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,37 @@
# libclang_rt.fuzzer_no_main library is composed manually.
# Function raises a fatal message if C compiler is not Clang.
#
# $ clang-15 -print-file-name=libclang_rt.fuzzer_no_main-x86_64.a
# $ /usr/lib/llvm-15/lib/clang/15.0.7/lib/linux/libclang_rt.fuzzer_no_main-x86_64.a
#
# On Linux installations libFuzzer library is typically located at:
#
# /usr/lib/<llvm-version>/lib/clang/<clang-version>/lib/linux/libclang_rt.fuzzer_no_main-<architecture>.a
#
# Location is LLVM_LIBRARY_DIRS/clang/<version>/lib/<OS>/,
# for example LLVM_LIBRARY_DIRS/clang/4.0.0/lib/darwin/.
#
# 1. https://llvm.org/docs/LibFuzzer.html#using-libfuzzer-as-a-library

function(SetFuzzerNoMainLibPath outvar)
if (NOT CMAKE_C_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "C compiler is not a Clang")
endif ()

set(ClangRTLibDir $ENV{FUZZER_NO_MAIN_LIBRARY})

if (NOT ClangRTLibDir)
string(REPLACE "." ";" VERSION_LIST ${CMAKE_C_COMPILER_VERSION})
list(GET VERSION_LIST 0 CLANG_VERSION_MAJOR)
# Clang <= 15: /usr/lib/llvm-xxx/lib/clang/X.Y.Z/lib/linux/
# Clang > 15: /usr/lib/llvm-xxx/lib/clang/X/lib/linux/
set(CLANG_VERSION ${CMAKE_C_COMPILER_VERSION})
if (CLANG_VERSION_MAJOR GREATER 15)
set(CLANG_VERSION ${CLANG_VERSION_MAJOR})
endif ()

if (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(ARCH "i386")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ARCH "x86_64")
endif ()

# find_package(LLVM 17 REQUIRED CONFIG)
# FIXME: Check presence of LLVM.
# set(LLVM_BASE "${LLVM_LIBRARY_DIRS}/clang/")

string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} OS_NAME)
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(ARCH "i386")
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ARCH "x86_64")
else ()
message(FATAL_ERROR "Unsupported architecture.")
endif ()

set(ClangRTLibDir "/usr/lib/clang/${CLANG_VERSION}/lib/${OS_NAME}/libclang_rt.fuzzer_no_main-${ARCH}.a")
set(LIB_FUZZER_NO_MAIN "libclang_rt.fuzzer_no_main-${ARCH}.a")
execute_process(COMMAND ${CMAKE_C_COMPILER} "-print-file-name=${LIB_FUZZER_NO_MAIN}"
RESULT_VARIABLE CMD_ERROR
OUTPUT_VARIABLE CMD_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (CMD_ERROR)
message(FATAL_ERROR "${CMD_ERROR}")
endif ()
set(${outvar} ${ClangRTLibDir} PARENT_SCOPE)
message(STATUS "[SetClangRTLibDir] ${outvar} is ${ClangRTLibDir}")
set(${outvar} ${CMD_OUTPUT} PARENT_SCOPE)
message(STATUS "[SetClangRTLib] ${outvar} is ${CMD_OUTPUT}")
endfunction()

0 comments on commit e788079

Please sign in to comment.