diff --git a/cmake/SetClangRTLibDir.cmake b/cmake/SetClangRTLibDir.cmake index 4ce099e..56debc5 100644 --- a/cmake/SetClangRTLibDir.cmake +++ b/cmake/SetClangRTLibDir.cmake @@ -4,13 +4,13 @@ # 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//lib/clang//lib/linux/libclang_rt.fuzzer_no_main-.a # -# Location is LLVM_LIBRARY_DIRS/clang//lib//, -# 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) @@ -18,32 +18,23 @@ function(SetFuzzerNoMainLibPath outvar) 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()