From 74a38fc3d837d276cdf1aa0eb217e69f2890f8ec Mon Sep 17 00:00:00 2001 From: Jun Zhang Date: Thu, 8 Jun 2023 19:03:59 +0800 Subject: [PATCH 1/2] Fix incorrect CMAKE_SOURCE_DIR usage When using solidity as a third-party library (include it into our project using FetchContent), we encountered a strange compilation error. For some reason, cmake considers the root directory of the project as the root directory of the dependency (solidity). This is because solidity is incorrectly using CMAKE_SOURCE_DIR variable, which should be PROJECT_SOURCE_DIR (The former one refers to the top-level source directory that contains a CMakeLists.txt, while the latter refers to the source directory of the most recent project() command) I've created a repo for demonstration (https://github.com/junaire/test-solidity-fetch-content) Signed-off-by: Jun Zhang --- CMakeLists.txt | 6 +++--- cmake/EthCompilerSettings.cmake | 2 +- cmake/fmtlib.cmake | 2 +- cmake/jsoncpp.cmake | 2 +- cmake/range-v3.cmake | 2 +- libsolutil/CMakeLists.txt | 2 +- libstdlib/CMakeLists.txt | 8 ++++---- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 85d2f64ce..6583f7cd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ list(APPEND CMAKE_MODULE_PATH ${ETH_CMAKE_DIR}) # Set the build type, if none was specified. if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - if(EXISTS "${CMAKE_SOURCE_DIR}/.git") + if(EXISTS "${PROJECT_SOURCE_DIR}/.git") set(DEFAULT_BUILD_TYPE "RelWithDebInfo") else() set(DEFAULT_BUILD_TYPE "Release") @@ -66,12 +66,12 @@ include(EthUtils) # Create license.h from LICENSE.txt and template # Converting to char array is required due to MSVC's string size limit. -file(READ ${CMAKE_SOURCE_DIR}/LICENSE.txt LICENSE_TEXT HEX) +file(READ ${PROJECT_SOURCE_DIR}/LICENSE.txt LICENSE_TEXT HEX) string(REGEX MATCHALL ".." LICENSE_TEXT "${LICENSE_TEXT}") string(REGEX REPLACE ";" ",\n\t0x" LICENSE_TEXT "${LICENSE_TEXT}") set(LICENSE_TEXT "0x${LICENSE_TEXT}") -configure_file("${CMAKE_SOURCE_DIR}/cmake/templates/license.h.in" include/license.h) +configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/license.h.in" include/license.h) include(EthOptions) configure_project(TESTS) diff --git a/cmake/EthCompilerSettings.cmake b/cmake/EthCompilerSettings.cmake index 4dd593458..6cfa864c1 100644 --- a/cmake/EthCompilerSettings.cmake +++ b/cmake/EthCompilerSettings.cmake @@ -28,7 +28,7 @@ if(PEDANTIC) endif() # Prevent the path of the source directory from ending up in the binary via __FILE__ macros. -eth_add_cxx_compiler_flag_if_supported("-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=/solidity") +eth_add_cxx_compiler_flag_if_supported("-fmacro-prefix-map=${PROJECT_SOURCE_DIR}=/solidity") # -Wpessimizing-move warns when a call to std::move would prevent copy elision # if the argument was not wrapped in a call. This happens when moving a local diff --git a/cmake/fmtlib.cmake b/cmake/fmtlib.cmake index 5ed196cea..2610424f6 100644 --- a/cmake/fmtlib.cmake +++ b/cmake/fmtlib.cmake @@ -3,7 +3,7 @@ include(FetchContent) FetchContent_Declare( fmtlib PREFIX "${CMAKE_BINARY_DIR}/deps" - DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/downloads" + DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads" DOWNLOAD_NAME fmt-8.0.1.tar.gz URL https://github.com/fmtlib/fmt/archive/8.0.1.tar.gz URL_HASH SHA256=b06ca3130158c625848f3fb7418f235155a4d389b2abc3a6245fb01cb0eb1e01 diff --git a/cmake/jsoncpp.cmake b/cmake/jsoncpp.cmake index 29b8f5f05..48a9d2b49 100644 --- a/cmake/jsoncpp.cmake +++ b/cmake/jsoncpp.cmake @@ -42,7 +42,7 @@ endif() ExternalProject_Add(jsoncpp-project PREFIX "${prefix}" - DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/downloads" + DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads" DOWNLOAD_NAME jsoncpp-1.9.3.tar.gz URL https://github.com/open-source-parsers/jsoncpp/archive/1.9.3.tar.gz URL_HASH SHA256=8593c1d69e703563d94d8c12244e2e18893eeb9a8a9f8aa3d09a327aa45c8f7d diff --git a/cmake/range-v3.cmake b/cmake/range-v3.cmake index 75ed5a92e..4f3225420 100644 --- a/cmake/range-v3.cmake +++ b/cmake/range-v3.cmake @@ -11,7 +11,7 @@ set(RANGE_V3_INCLUDE_DIR "${prefix}/include") ExternalProject_Add(range-v3-project PREFIX "${prefix}" - DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/downloads" + DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads" DOWNLOAD_NAME range-v3-0.12.0.tar.gz URL https://github.com/ericniebler/range-v3/archive/0.12.0.tar.gz URL_HASH SHA256=015adb2300a98edfceaf0725beec3337f542af4915cec4d0b89fa0886f4ba9cb diff --git a/libsolutil/CMakeLists.txt b/libsolutil/CMakeLists.txt index b368e12f9..201429bb7 100644 --- a/libsolutil/CMakeLists.txt +++ b/libsolutil/CMakeLists.txt @@ -46,7 +46,7 @@ set(sources add_library(solutil ${sources}) target_link_libraries(solutil PUBLIC jsoncpp Boost::boost Boost::filesystem Boost::system range-v3 fmt::fmt-header-only) -target_include_directories(solutil PUBLIC "${CMAKE_SOURCE_DIR}") +target_include_directories(solutil PUBLIC "${PROJECT_SOURCE_DIR}") add_dependencies(solutil solidity_BuildInfo.h) if(SOLC_LINK_STATIC) diff --git a/libstdlib/CMakeLists.txt b/libstdlib/CMakeLists.txt index a4523b99e..4a995c78f 100644 --- a/libstdlib/CMakeLists.txt +++ b/libstdlib/CMakeLists.txt @@ -1,18 +1,18 @@ # This will re-generate the headers if any file within src was modified. -set_directory_properties(PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/stdlib/src/) +set_directory_properties(PROPERTY CMAKE_CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/stdlib/src/) set(STDLIB stub) set(GENERATED_STDLIB_HEADERS) foreach(src IN LISTS STDLIB) - set(STDLIB_FILE ${CMAKE_SOURCE_DIR}/libstdlib/src/${src}.sol) + set(STDLIB_FILE ${PROJECT_SOURCE_DIR}/libstdlib/src/${src}.sol) file(READ ${STDLIB_FILE} STDLIB_FILE_CONTENT HEX) string(REGEX MATCHALL ".." STDLIB_FILE_CONTENT "${STDLIB_FILE_CONTENT}") list(REMOVE_ITEM STDLIB_FILE_CONTENT "0d") string(REGEX REPLACE ";" ",\n\t0x" STDLIB_FILE_CONTENT "${STDLIB_FILE_CONTENT}") set(STDLIB_FILE_CONTENT "0x${STDLIB_FILE_CONTENT}") set(STDLIB_FILE_NAME ${src}) - configure_file("${CMAKE_SOURCE_DIR}/libstdlib/stdlib.src.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h NEWLINE_STYLE LF @ONLY) + configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.src.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h NEWLINE_STYLE LF @ONLY) list(APPEND GENERATED_STDLIB_HEADERS ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h) endforeach() -configure_file("${CMAKE_SOURCE_DIR}/libstdlib/stdlib.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/stdlib.h NEWLINE_STYLE LF @ONLY) +configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/stdlib.h NEWLINE_STYLE LF @ONLY) From 8c5ecd1c0114aeb5e0ac24dc58218220d858237b Mon Sep 17 00:00:00 2001 From: Jun Zhang Date: Sat, 10 Jun 2023 00:21:55 +0800 Subject: [PATCH 2/2] Fix incorrectly CMAKE_BINARY_DIR usage Similiar to CMAKE_SOURCE_DIR. Using CMAKE_BINARY_DIR will put the generated header (stdlib.h) in build/include/solidity/libstdlib but it should start with build/third-party/solidity or the library can't find the header. Signed-off-by: Jun Zhang --- cmake/fmtlib.cmake | 2 +- cmake/jsoncpp.cmake | 2 +- cmake/range-v3.cmake | 2 +- libstdlib/CMakeLists.txt | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmake/fmtlib.cmake b/cmake/fmtlib.cmake index 2610424f6..792bf3b02 100644 --- a/cmake/fmtlib.cmake +++ b/cmake/fmtlib.cmake @@ -2,7 +2,7 @@ include(FetchContent) FetchContent_Declare( fmtlib - PREFIX "${CMAKE_BINARY_DIR}/deps" + PREFIX "${PROJECT_BINARY_DIR}/deps" DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads" DOWNLOAD_NAME fmt-8.0.1.tar.gz URL https://github.com/fmtlib/fmt/archive/8.0.1.tar.gz diff --git a/cmake/jsoncpp.cmake b/cmake/jsoncpp.cmake index 48a9d2b49..cf1cc656b 100644 --- a/cmake/jsoncpp.cmake +++ b/cmake/jsoncpp.cmake @@ -6,7 +6,7 @@ else() set(JSONCPP_CMAKE_COMMAND ${CMAKE_COMMAND}) endif() -set(prefix "${CMAKE_BINARY_DIR}/deps") +set(prefix "${PROJECT_BINARY_DIR}/deps") set(JSONCPP_LIBRARY "${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}jsoncpp${CMAKE_STATIC_LIBRARY_SUFFIX}") set(JSONCPP_INCLUDE_DIR "${prefix}/include") diff --git a/cmake/range-v3.cmake b/cmake/range-v3.cmake index 4f3225420..d2f54452b 100644 --- a/cmake/range-v3.cmake +++ b/cmake/range-v3.cmake @@ -6,7 +6,7 @@ else() set(RANGE_V3_CMAKE_COMMAND ${CMAKE_COMMAND}) endif() -set(prefix "${CMAKE_BINARY_DIR}/deps") +set(prefix "${PROJECT_BINARY_DIR}/deps") set(RANGE_V3_INCLUDE_DIR "${prefix}/include") ExternalProject_Add(range-v3-project diff --git a/libstdlib/CMakeLists.txt b/libstdlib/CMakeLists.txt index 4a995c78f..65a3a1cda 100644 --- a/libstdlib/CMakeLists.txt +++ b/libstdlib/CMakeLists.txt @@ -11,8 +11,8 @@ foreach(src IN LISTS STDLIB) string(REGEX REPLACE ";" ",\n\t0x" STDLIB_FILE_CONTENT "${STDLIB_FILE_CONTENT}") set(STDLIB_FILE_CONTENT "0x${STDLIB_FILE_CONTENT}") set(STDLIB_FILE_NAME ${src}) - configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.src.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h NEWLINE_STYLE LF @ONLY) - list(APPEND GENERATED_STDLIB_HEADERS ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h) + configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.src.h.in" ${PROJECT_BINARY_DIR}/include/libstdlib/${src}.h NEWLINE_STYLE LF @ONLY) + list(APPEND GENERATED_STDLIB_HEADERS ${PROJECT_BINARY_DIR}/include/libstdlib/${src}.h) endforeach() -configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/stdlib.h NEWLINE_STYLE LF @ONLY) +configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.h.in" ${PROJECT_BINARY_DIR}/include/libstdlib/stdlib.h NEWLINE_STYLE LF @ONLY)