Skip to content

Commit 74a38fc

Browse files
committed
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 <[email protected]>
1 parent facc380 commit 74a38fc

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ list(APPEND CMAKE_MODULE_PATH ${ETH_CMAKE_DIR})
55

66
# Set the build type, if none was specified.
77
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
8-
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
8+
if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
99
set(DEFAULT_BUILD_TYPE "RelWithDebInfo")
1010
else()
1111
set(DEFAULT_BUILD_TYPE "Release")
@@ -66,12 +66,12 @@ include(EthUtils)
6666

6767
# Create license.h from LICENSE.txt and template
6868
# Converting to char array is required due to MSVC's string size limit.
69-
file(READ ${CMAKE_SOURCE_DIR}/LICENSE.txt LICENSE_TEXT HEX)
69+
file(READ ${PROJECT_SOURCE_DIR}/LICENSE.txt LICENSE_TEXT HEX)
7070
string(REGEX MATCHALL ".." LICENSE_TEXT "${LICENSE_TEXT}")
7171
string(REGEX REPLACE ";" ",\n\t0x" LICENSE_TEXT "${LICENSE_TEXT}")
7272
set(LICENSE_TEXT "0x${LICENSE_TEXT}")
7373

74-
configure_file("${CMAKE_SOURCE_DIR}/cmake/templates/license.h.in" include/license.h)
74+
configure_file("${PROJECT_SOURCE_DIR}/cmake/templates/license.h.in" include/license.h)
7575

7676
include(EthOptions)
7777
configure_project(TESTS)

cmake/EthCompilerSettings.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ if(PEDANTIC)
2828
endif()
2929

3030
# Prevent the path of the source directory from ending up in the binary via __FILE__ macros.
31-
eth_add_cxx_compiler_flag_if_supported("-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=/solidity")
31+
eth_add_cxx_compiler_flag_if_supported("-fmacro-prefix-map=${PROJECT_SOURCE_DIR}=/solidity")
3232

3333
# -Wpessimizing-move warns when a call to std::move would prevent copy elision
3434
# if the argument was not wrapped in a call. This happens when moving a local

cmake/fmtlib.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include(FetchContent)
33
FetchContent_Declare(
44
fmtlib
55
PREFIX "${CMAKE_BINARY_DIR}/deps"
6-
DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/downloads"
6+
DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads"
77
DOWNLOAD_NAME fmt-8.0.1.tar.gz
88
URL https://github.com/fmtlib/fmt/archive/8.0.1.tar.gz
99
URL_HASH SHA256=b06ca3130158c625848f3fb7418f235155a4d389b2abc3a6245fb01cb0eb1e01

cmake/jsoncpp.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ endif()
4242

4343
ExternalProject_Add(jsoncpp-project
4444
PREFIX "${prefix}"
45-
DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/downloads"
45+
DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads"
4646
DOWNLOAD_NAME jsoncpp-1.9.3.tar.gz
4747
URL https://github.com/open-source-parsers/jsoncpp/archive/1.9.3.tar.gz
4848
URL_HASH SHA256=8593c1d69e703563d94d8c12244e2e18893eeb9a8a9f8aa3d09a327aa45c8f7d

cmake/range-v3.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set(RANGE_V3_INCLUDE_DIR "${prefix}/include")
1111

1212
ExternalProject_Add(range-v3-project
1313
PREFIX "${prefix}"
14-
DOWNLOAD_DIR "${CMAKE_SOURCE_DIR}/deps/downloads"
14+
DOWNLOAD_DIR "${PROJECT_SOURCE_DIR}/deps/downloads"
1515
DOWNLOAD_NAME range-v3-0.12.0.tar.gz
1616
URL https://github.com/ericniebler/range-v3/archive/0.12.0.tar.gz
1717
URL_HASH SHA256=015adb2300a98edfceaf0725beec3337f542af4915cec4d0b89fa0886f4ba9cb

libsolutil/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ set(sources
4646

4747
add_library(solutil ${sources})
4848
target_link_libraries(solutil PUBLIC jsoncpp Boost::boost Boost::filesystem Boost::system range-v3 fmt::fmt-header-only)
49-
target_include_directories(solutil PUBLIC "${CMAKE_SOURCE_DIR}")
49+
target_include_directories(solutil PUBLIC "${PROJECT_SOURCE_DIR}")
5050
add_dependencies(solutil solidity_BuildInfo.h)
5151

5252
if(SOLC_LINK_STATIC)

libstdlib/CMakeLists.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# This will re-generate the headers if any file within src was modified.
2-
set_directory_properties(PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/stdlib/src/)
2+
set_directory_properties(PROPERTY CMAKE_CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/stdlib/src/)
33

44
set(STDLIB stub)
55
set(GENERATED_STDLIB_HEADERS)
66
foreach(src IN LISTS STDLIB)
7-
set(STDLIB_FILE ${CMAKE_SOURCE_DIR}/libstdlib/src/${src}.sol)
7+
set(STDLIB_FILE ${PROJECT_SOURCE_DIR}/libstdlib/src/${src}.sol)
88
file(READ ${STDLIB_FILE} STDLIB_FILE_CONTENT HEX)
99
string(REGEX MATCHALL ".." STDLIB_FILE_CONTENT "${STDLIB_FILE_CONTENT}")
1010
list(REMOVE_ITEM STDLIB_FILE_CONTENT "0d")
1111
string(REGEX REPLACE ";" ",\n\t0x" STDLIB_FILE_CONTENT "${STDLIB_FILE_CONTENT}")
1212
set(STDLIB_FILE_CONTENT "0x${STDLIB_FILE_CONTENT}")
1313
set(STDLIB_FILE_NAME ${src})
14-
configure_file("${CMAKE_SOURCE_DIR}/libstdlib/stdlib.src.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h NEWLINE_STYLE LF @ONLY)
14+
configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.src.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h NEWLINE_STYLE LF @ONLY)
1515
list(APPEND GENERATED_STDLIB_HEADERS ${CMAKE_BINARY_DIR}/include/libstdlib/${src}.h)
1616
endforeach()
1717

18-
configure_file("${CMAKE_SOURCE_DIR}/libstdlib/stdlib.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/stdlib.h NEWLINE_STYLE LF @ONLY)
18+
configure_file("${PROJECT_SOURCE_DIR}/libstdlib/stdlib.h.in" ${CMAKE_BINARY_DIR}/include/libstdlib/stdlib.h NEWLINE_STYLE LF @ONLY)

0 commit comments

Comments
 (0)