Skip to content

Commit 2f5f71d

Browse files
committed
Fix src_root and src_location tests (#164)
1 parent 8b41926 commit 2f5f71d

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

util/CMakeLists.txt

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,73 @@ elseif (WIN32 AND CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
701701
)
702702
endif()
703703

704+
#[=============================================================================[
705+
Since in certain circumstances the __FILE__ macro expands to a relative path,
706+
some internal macros that trims the source or binary prefix of the file path
707+
may not work properly, if we pass these prefixes as absolute paths via
708+
ARCADIA_ROOT and ARCADIA_BUILD_ROOT. This function deduces the proper arcadia
709+
root paths, either relative or absolute, and put them in the cached variables
710+
ARCADIA_ROOT and ARCADIA_BUILD_ROOT.
711+
#]=============================================================================]
712+
function(deduce_arcadia_roots)
713+
if (DEFINED CACHE{ARCADIA_ROOT} AND DEFINED CACHE{ARCADIA_BUILD_ROOT})
714+
return()
715+
endif()
716+
717+
set(helperDir "${YDB_SDK_BINARY_DIR}/arcadia_roots_helper")
718+
set(sourceFile "arcadia_roots_helper.cpp")
719+
set(cxxSrcFilePath "${helperDir}/src_cxx_file_path.txt")
720+
set(cxxBinFilePath "${helperDir}/bin_cxx_file_path.txt")
721+
722+
if (NOT EXISTS "${helperDir}/${sourceFile}")
723+
set(sourceContent
724+
[=[
725+
#include <fstream>
726+
int main() {
727+
std::ofstream file("@cxxFilePath@");
728+
file << __FILE__;
729+
}
730+
]=]
731+
)
732+
file(WRITE "${helperDir}/${sourceFile}" "${sourceContent}")
733+
endif()
734+
735+
foreach (varList
736+
"${cxxSrcFilePath};${YDB_SDK_SOURCE_DIR};ARCADIA_ROOT"
737+
"${cxxBinFilePath};${YDB_SDK_BINARY_DIR};ARCADIA_BUILD_ROOT"
738+
)
739+
list(GET varList 0 cxxFilePath)
740+
list(GET varList 1 sourceDir)
741+
list(GET varList 2 arcadiaCacheVariableName)
742+
743+
configure_file(
744+
"${helperDir}/${sourceFile}"
745+
"${sourceDir}/${sourceFile}"
746+
@ONLY
747+
)
748+
749+
try_run(runResultVar compileResultVar
750+
"${helperDir}/build" "${sourceDir}/${sourceFile}"
751+
)
752+
if ((NOT runResultVar EQUAL "0") OR NOT compileResultVar)
753+
message(FATAL_ERROR "Deducing ${arcadiaCacheVariableName} failed.")
754+
endif()
755+
756+
file(READ "${cxxFilePath}" cxxFilePathContent)
757+
file(TO_CMAKE_PATH "${cxxFilePathContent}" cxxFilePathContent)
758+
get_filename_component(rootDir "${cxxFilePathContent}" DIRECTORY)
759+
set(${arcadiaCacheVariableName} "${rootDir}" CACHE INTERNAL "")
760+
761+
file(REMOVE "${sourceDir}/${sourceFile}")
762+
endforeach()
763+
endfunction()
764+
765+
deduce_arcadia_roots()
766+
704767
# These definitions are needed to `util/system/src_root.h` works properly
705768
target_compile_definitions(yutil PUBLIC
706-
ARCADIA_ROOT=${YDB_SDK_SOURCE_DIR}
707-
ARCADIA_BUILD_ROOT=${YDB_SDK_BINARY_DIR}
769+
ARCADIA_ROOT=${ARCADIA_ROOT}
770+
ARCADIA_BUILD_ROOT=${ARCADIA_BUILD_ROOT}
708771
)
709772

710773
_ydb_sdk_install_targets(TARGETS yutil)

0 commit comments

Comments
 (0)