Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 5 additions & 3 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ cmake_minimum_required(VERSION 3.13.0)
project(example LANGUAGES C CXX ASM)

# Download xrepo.cmake if not exists in build directory.
if(NOT EXISTS "${CMAKE_BINARY_DIR}/xrepo.cmake")
# You should use "${CMAKE_BINARY_DIR}/xrepo.cmake"
if(NOT EXISTS "../xrepo.cmake")
message(STATUS "Downloading xrepo.cmake from https://github.com/xmake-io/xrepo-cmake/")
# mirror https://cdn.jsdelivr.net/gh/xmake-io/xrepo-cmake@main/xrepo.cmake
file(DOWNLOAD "https://raw.githubusercontent.com/xmake-io/xrepo-cmake/main/xrepo.cmake"
"${CMAKE_BINARY_DIR}/xrepo.cmake"
"../xrepo.cmake"
TLS_VERIFY ON)
endif()

# Include xrepo.cmake so we can use xrepo_package function.
include(${CMAKE_BINARY_DIR}/xrepo.cmake)
# You should use "${CMAKE_BINARY_DIR}/xrepo.cmake"
include(../xrepo.cmake)

# Call `xrepo_package` function to use default pcre2
xrepo_package("pcre2" MODE debug)
Expand Down
3 changes: 1 addition & 2 deletions scripts/test-unix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cmake -DXREPO_PACKAGE_VERBOSE=ON example | tee $output
grep -E 'mode=debug pcre2' $output
grep -E "pcre2_INCLUDE_DIRS" $output
grep -E "pcre2_LIBRARY_DIRS" $output
grep -E "pcre2_LIBRARIES" $output
grep -E "pcre2_LINK_LIBRARIES" $output
grep -E "pcre2_DEFINITIONS" $output
grep -E "gflags prepend to CMAKE_PREFIX_PATH" $output
grep -E "glog prepend to CMAKE_PREFIX_PATH" $output
Expand Down Expand Up @@ -42,4 +42,3 @@ grep -E "pcre2 $match_cached_output" $output
grep -E "gflags 2.2.2 $match_cached_output" $output
grep -E "example/packages/glog.lua" $output
grep -E "myzlib $match_cached_output" $output

66 changes: 39 additions & 27 deletions xrepo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ set(XREPO_XMAKEFILE "" CACHE STRING "Xmake script file of Xrepo package")
# If specified, include all dependent libraries' settings in various
# variables. Also add all dependent libraries' install dir to
# CMAKE_PREFIX_PATH.
# USE_ABSOLUTE_LIBS: optional
# If specified, forces the linker to use absolute paths to library files rather than
# relying on the -l flag which only specifies the library name. This can be particularly
# useful when you need to ensure that the linker finds the correct library version,
# especially in environments where multiple versions of the same library might be present.
# MODE: optional, debug|release
# Pass `--mode` option to xrepo install command. If not specified,
# `--mode` option is not passed.
Expand All @@ -47,6 +52,7 @@ set(XREPO_XMAKEFILE "" CACHE STRING "Xmake script file of Xrepo package")
# [CONFIGS feature1=true,feature2=false]
# [CONFIGS path/to/script.lua]
# [DEPS]
# [USE_ABSOLUTE_LIBS]
# [MODE debug|release]
# [OUTPUT verbose|diagnosis|quiet]
# [DIRECTORY_SCOPE]
Expand Down Expand Up @@ -255,7 +261,7 @@ function(xrepo_package package)
return()
endif()

set(options "DIRECTORY_SCOPE;DEPS")
set(options "DIRECTORY_SCOPE;DEPS;USE_ABSOLUTE_LIBS")
set(one_value_args CONFIGS MODE OUTPUT ALIAS)
cmake_parse_arguments(ARG "${options}" "${one_value_args}" "" ${ARGN})

Expand Down Expand Up @@ -400,9 +406,9 @@ function(xrepo_target_packages target)
message(STATUS "xrepo: target_link_directories(${target} ${_visibility} ${${package_name}_LIBRARY_DIRS})")
target_link_directories(${target} ${_visibility} ${${package_name}_LIBRARY_DIRS})
endif()
if((DEFINED ${package_name}_LIBRARIES) AND (NOT ARG_NO_LINK_LIBRARIES))
message(STATUS "xrepo: target_link_libraries(${target} ${_visibility} ${${package_name}_LIBRARIES})")
target_link_libraries(${target} ${_visibility} ${${package_name}_LIBRARIES})
if((DEFINED ${package_name}_LINK_LIBRARIES) AND (NOT ARG_NO_LINK_LIBRARIES))
message(STATUS "xrepo: target_link_libraries(${target} ${_visibility} ${${package_name}_LINK_LIBRARIES})")
target_link_libraries(${target} ${_visibility} ${${package_name}_LINK_LIBRARIES})
endif()
if((DEFINED ${package_name}_SYS_LIBRARIES) AND (NOT ARG_NO_LINK_LIBRARIES))
message(STATUS "xrepo: target_link_libraries(${target} ${_visibility} ${${package_name}_SYS_LIBRARIES})")
Expand Down Expand Up @@ -530,36 +536,41 @@ macro(_xrepo_fetch_json)
endif()
endif()

# Loop over linkdirs.
string(JSON linkdirs_type ERROR_VARIABLE linkdirs_error TYPE ${json_output} ${idx} "linkdirs")
if("${linkdirs_type}" STREQUAL "STRING")
string(JSON dir GET ${json_output} ${idx} "linkdirs")
list(APPEND linkdirs ${dir})
elseif("${linkdirs_type}" MATCHES "ARRAY|OBJECT")
string(JSON linkdirs_len ERROR_VARIABLE linkdirs_error LENGTH ${json_output} ${idx} "linkdirs")
if("${linkdirs_error}" STREQUAL "NOTFOUND" AND NOT "${linkdirs_len}" EQUAL 0)
math(EXPR linkdirs_end "${linkdirs_len} - 1")
foreach(linkdirs_idx RANGE 0 ${linkdirs_end})
string(JSON dir GET ${json_output} ${idx} "linkdirs" ${linkdirs_idx})
list(APPEND linkdirs ${dir})
#message(STATUS "xrepo DEBUG: linkdirs ${idx} ${linkdirs_idx} ${dir}")
endforeach()
if (NOT ARG_USE_ABSOLUTE_LIBS)
set (links_tag "links")
# Loop over linkdirs.
string(JSON linkdirs_type ERROR_VARIABLE linkdirs_error TYPE ${json_output} ${idx} "linkdirs")
if("${linkdirs_type}" STREQUAL "STRING")
string(JSON dir GET ${json_output} ${idx} "linkdirs")
list(APPEND linkdirs ${dir})
elseif("${linkdirs_type}" MATCHES "ARRAY|OBJECT")
string(JSON linkdirs_len ERROR_VARIABLE linkdirs_error LENGTH ${json_output} ${idx} "linkdirs")
if("${linkdirs_error}" STREQUAL "NOTFOUND" AND NOT "${linkdirs_len}" EQUAL 0)
math(EXPR linkdirs_end "${linkdirs_len} - 1")
foreach(linkdirs_idx RANGE 0 ${linkdirs_end})
string(JSON dir GET ${json_output} ${idx} "linkdirs" ${linkdirs_idx})
list(APPEND linkdirs ${dir})
#message(STATUS "xrepo DEBUG: linkdirs ${idx} ${linkdirs_idx} ${dir}")
endforeach()
endif()
endif()
else()
set (links_tag "libfiles")
endif()

# Loop over links.
string(JSON links_type ERROR_VARIABLE links_error TYPE ${json_output} ${idx} "links")
string(JSON links_type ERROR_VARIABLE links_error TYPE ${json_output} ${idx} ${links_tag})
if("${links_type}" STREQUAL "STRING")
string(JSON dir GET ${json_output} ${idx} "links")
list(APPEND links ${dir})
string(JSON libfile GET ${json_output} ${idx} ${links_tag})
list(APPEND links ${libfile})
elseif("${links_type}" MATCHES "ARRAY|OBJECT")
string(JSON links_len ERROR_VARIABLE links_error LENGTH ${json_output} ${idx} "links")
string(JSON links_len ERROR_VARIABLE links_error LENGTH ${json_output} ${idx} ${links_tag})
if("${links_error}" STREQUAL "NOTFOUND" AND NOT "${links_len}" EQUAL 0)
math(EXPR links_end "${links_len} - 1")
foreach(links_idx RANGE 0 ${links_end})
string(JSON dir GET ${json_output} ${idx} "links" ${links_idx})
list(APPEND links ${dir})
#message(STATUS "xrepo DEBUG: links ${idx} ${links_idx} ${dir}")
string(JSON libfile GET ${json_output} ${idx} ${links_tag} ${links_idx})
list(APPEND links ${libfile})
#message(STATUS "xrepo DEBUG: links ${idx} ${links_idx} ${libfile}")
endforeach()
endif()
endif()
Expand Down Expand Up @@ -618,9 +629,10 @@ macro(_xrepo_fetch_json)
endif()

if(DEFINED links)
set(${package_name}_LINK_LIBRARIES "${links}" CACHE INTERNAL "")
set(${package_name}_LIBRARIES "${links}" CACHE INTERNAL "")
list(APPEND xrepo_vars_${package_name} ${package_name}_LIBRARIES)
message(STATUS "xrepo: ${package_name}_LIBRARIES ${${package_name}_LIBRARIES}")
list(APPEND xrepo_vars_${package_name} ${package_name}_LINK_LIBRARIES)
message(STATUS "xrepo: ${package_name}_LINK_LIBRARIES ${${package_name}_LINK_LIBRARIES}")
else()
message(STATUS "xrepo fetch --json: ${package_name} links not found")
endif()
Expand Down
Loading