Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMake: improve search for Zycore #461

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 53 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ option(ZYDIS_LIBFUZZER

# Dependencies
option(ZYAN_SYSTEM_ZYCORE
"Use system Zycore library"
"Force using system installed Zycore library"
OFF)
set(ZYAN_ZYCORE_PATH
"${CMAKE_CURRENT_LIST_DIR}/dependencies/zycore"
Expand All @@ -83,35 +83,62 @@ set(ZYAN_ZYCORE_PATH
# Dependencies #
# =============================================================================================== #

if (ZYAN_SYSTEM_ZYCORE)
find_package(Zycore REQUIRED)
else ()
# Try to initialize the Zycore submodule using Git
if (NOT EXISTS "${ZYAN_ZYCORE_PATH}/CMakeLists.txt" AND
"${ZYAN_ZYCORE_PATH}" STREQUAL "${CMAKE_CURRENT_LIST_DIR}/dependencies/zycore")
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
# Tries to make Zycore available.
#
# Priorities:
#
# - ZYAN_ZYCORE_PATH specified path always takes maximum precedence if it exists.
# - Default value is the sub-module path. So if the sub-module is present, we pick that.
# Allows hacking on Zydis/Zycore even if a Zydis OS package is installed.
# - Look for a system-installed Zycore package (via find_package).
# - If git is installed & this is a git repository, try cloning the sub-module.
# - Give up.
#
# This is in a function so we can elegantly early-exit once the library is found.
function (locate_zycore)
if (NOT ${ZYAN_SYSTEM_ZYCORE} AND EXISTS "${ZYAN_ZYCORE_PATH}/CMakeLists.txt")
message(VERBOSE "Using ZYAN_ZYCORE_PATH specified Zycore")
add_subdirectory(${ZYAN_ZYCORE_PATH} "zycore" EXCLUDE_FROM_ALL)
return ()
endif ()

if (NOT EXISTS "${ZYAN_ZYCORE_PATH}/CMakeLists.txt")
message(
FATAL_ERROR
"Can't find zycore submodule. Please make sure to clone the repo recursively.\n"
"You can fix this by running\n"
" git submodule update --init\n"
"or by cloning using\n"
" git clone --recursive <url>\n"
"Alternatively, you can manually clone zycore to some path and set ZYDIS_ZYCORE_PATH."
)
if (NOT "${ZYAN_ZYCORE_PATH}" STREQUAL "${CMAKE_CURRENT_LIST_DIR}/dependencies/zycore")
message(FATAL_ERROR "No CMake project found at explicitly set ZYAN_ZYCORE_PATH")
endif ()

add_subdirectory(${ZYAN_ZYCORE_PATH} "zycore" EXCLUDE_FROM_ALL)
endif ()
find_package(Zycore QUIET)
if (Zycore_FOUND)
message(VERBOSE "Using system Zycore")
return ()
endif ()

if (ZYAN_SYSTEM_ZYCORE)
message(FATAL_ERROR "ZYAN_SYSTEM_ZYCORE set but no system-installed Zycore found")
endif ()

find_package(Git QUIET)
Copy link
Member

@flobernd flobernd Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could possibly change this block to the newer CMake 'FetchContent' APIs. However, not sure if we would have to bump our CMake version requirement to do so.

Edit: It might introduce some overhead, as we would have to determine the correct commit hash, that's used for the Git submodule.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we'd have to maintain a separate pinned revision in the CMakeLists then, no? With the existing approach we're using the revision stored in git's meta-info. Or does FetchContent explicitly support this somehow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would have to either maintain it in two places or extract the hash from the repository config by parsing the Git files.

Not sure if it's worth it. Probably not. Git should be installed on most dev machines anyways.

if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
message(VERBOSE "Pulling Zycore submodule with git.")
execute_process(
COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
add_subdirectory(${ZYAN_ZYCORE_PATH} "zycore" EXCLUDE_FROM_ALL)
return ()
endif()

message(
FATAL_ERROR
"Can't find Zycore. Please make sure to clone the repo recursively.\n"
"You can fix this by running\n"
" git submodule update --init\n"
"or by cloning using\n"
" git clone --recursive <url>\n"
"Alternatively, you can manually clone zycore to some path and set ZYDIS_ZYCORE_PATH."
)
endfunction ()

locate_zycore()

# =============================================================================================== #
# Library configuration #
Expand Down
Loading