Skip to content

Commit df61080

Browse files
committed
Nix toolchain support
1 parent a5ada46 commit df61080

File tree

4 files changed

+124
-15
lines changed

4 files changed

+124
-15
lines changed

vcpkg_overlay/CMakePresets.json

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@
9898
},
9999
"vendor": {
100100
"microsoft.com/VisualStudioSettings/CMake/1.0": {
101-
"hostOS": [
102-
"Windows"
103-
]
101+
"hostOS": ["Windows"]
104102
}
105103
},
106104
"cacheVariables": {
@@ -147,10 +145,7 @@
147145
{
148146
"name": "triplet-x64-windows-static-inteloneapi",
149147
"hidden": true,
150-
"inherits": [
151-
"vcpkg-base",
152-
"inteloneapi-env-win"
153-
],
148+
"inherits": ["vcpkg-base", "inteloneapi-env-win"],
154149
"cacheVariables": {
155150
"VCPKG_TARGET_TRIPLET": "x64-windows-static-inteloneapi",
156151
"VCPKG_INSTALLED_DIR": "${sourceDir}/build/vcpkgs/x64-windows-static-inteloneapi",
@@ -266,6 +261,25 @@
266261
"rhs": "Linux"
267262
}
268263
},
264+
{
265+
"name": "triplet-x64-linux-nix",
266+
"inherits": "vcpkg-base",
267+
"hidden": true,
268+
"cacheVariables": {
269+
"VCPKG_TARGET_TRIPLET": "x64-linux-nix",
270+
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/deps/infra/vcpkg_overlay/triplets/toolchain-linux-nix.cmake",
271+
"VCPKG_INSTALLED_DIR": "${sourceDir}/build/vcpkgs/x64-linux-nix",
272+
"VCPKG_TARGET_ARCHITECTURE": "x64",
273+
"VCPKG_CRT_LINKAGE": "dynamic",
274+
"VCPKG_LIBRARY_LINKAGE": "static",
275+
"VCPKG_LINKER_FLAGS": "-static-libstdc++ -static-libgcc -fuse-ld=gold"
276+
},
277+
"condition": {
278+
"type": "equals",
279+
"lhs": "${hostSystemName}",
280+
"rhs": "Linux"
281+
}
282+
},
269283
{
270284
"name": "triplet-x64-linux-cluster",
271285
"inherits": "vcpkg-base",
@@ -287,10 +301,7 @@
287301
},
288302
{
289303
"name": "triplet-x64-linux-inteloneapi",
290-
"inherits": [
291-
"vcpkg-base",
292-
"inteloneapi-env"
293-
],
304+
"inherits": ["vcpkg-base", "inteloneapi-env"],
294305
"hidden": true,
295306
"environment": {
296307
"VCPKG_DEFAULT_HOST_TRIPLET": "x64-linux-inteloneapi"
@@ -342,4 +353,4 @@
342353
}
343354
}
344355
]
345-
}
356+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
if(NOT _VCPKG_LINUX_TOOLCHAIN)
2+
set(_VCPKG_LINUX_TOOLCHAIN 1)
3+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
4+
set(CMAKE_CROSSCOMPILING OFF CACHE BOOL "")
5+
endif()
6+
set(CMAKE_SYSTEM_NAME Linux CACHE STRING "")
7+
if(VCPKG_TARGET_ARCHITECTURE STREQUAL "x64")
8+
set(CMAKE_SYSTEM_PROCESSOR x86_64 CACHE STRING "")
9+
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "x86")
10+
set(CMAKE_SYSTEM_PROCESSOR x86 CACHE STRING "")
11+
string(APPEND VCPKG_C_FLAGS " -m32")
12+
string(APPEND VCPKG_CXX_FLAGS " -m32")
13+
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm")
14+
set(CMAKE_SYSTEM_PROCESSOR armv7l CACHE STRING "")
15+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
16+
if(NOT DEFINED CMAKE_CXX_COMPILER)
17+
set(CMAKE_CXX_COMPILER "arm-linux-gnueabihf-g++")
18+
endif()
19+
if(NOT DEFINED CMAKE_C_COMPILER)
20+
set(CMAKE_C_COMPILER "arm-linux-gnueabihf-gcc")
21+
endif()
22+
message(STATUS "Cross compiling arm on host x86_64, use cross compiler: ${CMAKE_CXX_COMPILER}/${CMAKE_C_COMPILER}")
23+
endif()
24+
elseif(VCPKG_TARGET_ARCHITECTURE STREQUAL "arm64")
25+
set(CMAKE_SYSTEM_PROCESSOR aarch64 CACHE STRING "")
26+
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
27+
if(NOT DEFINED CMAKE_CXX_COMPILER)
28+
set(CMAKE_CXX_COMPILER "aarch64-linux-gnu-g++")
29+
endif()
30+
if(NOT DEFINED CMAKE_C_COMPILER)
31+
set(CMAKE_C_COMPILER "aarch64-linux-gnu-gcc")
32+
endif()
33+
message(STATUS "Cross compiling arm64 on host x86_64, use cross compiler: ${CMAKE_CXX_COMPILER}/${CMAKE_C_COMPILER}")
34+
endif()
35+
endif()
36+
37+
# Add Nix sysroot roots so find_library()/find_path()/find_package() can see glibc & compiler runtimes.
38+
# These env vars must be set by the Nix shell.
39+
foreach(root
40+
NIX_CC_LIB # e.g. /nix/store/...-gcc-*/libstdc++ runtime (libstdc++, libgcc_s)
41+
NIX_GLIBC_LIB # e.g. /nix/store/...-glibc-*/lib (libm, libc, crt)
42+
NIX_CC_DEV # e.g. /nix/store/...-gcc-*/include
43+
NIX_GLIBC_DEV # e.g. /nix/store/...-glibc-*/include
44+
)
45+
if(DEFINED ENV{${root}} AND NOT "$ENV{${root}}" STREQUAL "")
46+
list(APPEND CMAKE_FIND_ROOT_PATH "$ENV{${root}}")
47+
endif()
48+
endforeach()
49+
50+
# Search both within ROOT_PATH and defaults (be permissive).
51+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
52+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
53+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
54+
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
55+
56+
# Also extend classic search vars to help plain find_* calls.
57+
if(DEFINED ENV{NIX_CC_LIB})
58+
list(APPEND CMAKE_LIBRARY_PATH "$ENV{NIX_CC_LIB}/lib")
59+
endif()
60+
if(DEFINED ENV{NIX_GLIBC_LIB})
61+
list(APPEND CMAKE_LIBRARY_PATH "$ENV{NIX_GLIBC_LIB}/lib")
62+
endif()
63+
if(DEFINED ENV{NIX_CC_DEV})
64+
list(APPEND CMAKE_INCLUDE_PATH "$ENV{NIX_CC_DEV}/include")
65+
endif()
66+
if(DEFINED ENV{NIX_GLIBC_DEV})
67+
list(APPEND CMAKE_INCLUDE_PATH "$ENV{NIX_GLIBC_DEV}/include")
68+
endif()
69+
70+
get_property( _CMAKE_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE )
71+
if(NOT _CMAKE_IN_TRY_COMPILE)
72+
string(APPEND CMAKE_C_FLAGS_INIT " -fPIC ${VCPKG_C_FLAGS} ")
73+
string(APPEND CMAKE_CXX_FLAGS_INIT " -fPIC ${VCPKG_CXX_FLAGS} ")
74+
string(APPEND CMAKE_C_FLAGS_DEBUG_INIT " ${VCPKG_C_FLAGS_DEBUG} ")
75+
string(APPEND CMAKE_CXX_FLAGS_DEBUG_INIT " ${VCPKG_CXX_FLAGS_DEBUG} ")
76+
string(APPEND CMAKE_C_FLAGS_RELEASE_INIT " ${VCPKG_C_FLAGS_RELEASE} ")
77+
string(APPEND CMAKE_CXX_FLAGS_RELEASE_INIT " ${VCPKG_CXX_FLAGS_RELEASE} ")
78+
79+
string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT " ${VCPKG_LINKER_FLAGS} ")
80+
string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${VCPKG_LINKER_FLAGS} ")
81+
if(VCPKG_CRT_LINKAGE STREQUAL "static")
82+
string(APPEND CMAKE_SHARED_LINKER_FLAGS_INIT "-static ")
83+
string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT "-static ")
84+
endif()
85+
string(APPEND CMAKE_SHARED_LINKER_FLAGS_DEBUG_INIT " ${VCPKG_LINKER_FLAGS_DEBUG} ")
86+
string(APPEND CMAKE_EXE_LINKER_FLAGS_DEBUG_INIT " ${VCPKG_LINKER_FLAGS_DEBUG} ")
87+
string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE_INIT " ${VCPKG_LINKER_FLAGS_RELEASE} ")
88+
string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE_INIT " ${VCPKG_LINKER_FLAGS_RELEASE} ")
89+
endif()
90+
endif()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
set(VCPKG_TARGET_ARCHITECTURE x64)
2+
set(VCPKG_CRT_LINKAGE dynamic)
3+
set(VCPKG_LIBRARY_LINKAGE static)
4+
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
5+
6+
set(LINKER_EXECUTABLE ld.gold)
7+
set(VCPKG_LINKER_FLAGS "-static-libstdc++ -static-libgcc -fuse-ld=gold")
8+
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE "${CMAKE_CURRENT_LIST_DIR}/toolchain-linux-nix.cmake")

vcpkg_overlay/vcpkg.just

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# detect the vcpkg triplet based on the system information
22
windows_triplet := "x64-windows-static"
33

4-
VCPKG_DEFAULT_TRIPLET := if os_family() == "windows" {
4+
VCPKG_DEFAULT_TRIPLET := env_var_or_default('VCPKG_DEFAULT_TRIPLET', if os_family() == "windows" {
55
windows_triplet
66
} else if os() == "macos" {
77
if arch() == "aarch64" {
88
"arm64-osx-homebrew"
99
} else { "x64-osx-homebrew" }
1010
} else {
1111
"x64-linux"
12-
}
12+
})
1313

1414
cmake_preset := VCPKG_DEFAULT_TRIPLET
1515

1616
VCPKG_DEFAULT_HOST_TRIPLET := VCPKG_DEFAULT_TRIPLET
17-
vcpkg_root := env('VCPKG_ROOT', join(justfile_directory(), "./deps/vcpkg"))
17+
vcpkg_root := env_var_or_default('VCPKG_ROOT', join(justfile_directory(), "./deps/vcpkg"))
1818
in_git_repo := path_exists(join(justfile_directory(), ".git"))
1919

2020
clone_vcpkg:

0 commit comments

Comments
 (0)