Support cross-compiling for Windows

This commit is contained in:
Přemysl Eric Janouch 2024-11-25 05:24:02 +01:00
parent 0283189070
commit b5dec466c6
Signed by: p
GPG Key ID: A0420B94F92B9493
3 changed files with 110 additions and 28 deletions

View File

@ -11,6 +11,23 @@ if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-function")
endif ()
if (WIN32 AND CMAKE_CROSSCOMPILING)
set (win32_deps_root "${PROJECT_SOURCE_DIR}")
set (win32_deps_prefix "${win32_deps_root}/mingw64")
list (APPEND CMAKE_PREFIX_PATH "${win32_deps_prefix}")
list (APPEND CMAKE_INCLUDE_PATH "${win32_deps_prefix}/lib")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mms-bitfields")
list (APPEND CMAKE_FIND_ROOT_PATH ${win32_deps_prefix})
# Relativize prefixes, and bar pkg-config from looking up host libraries
set (ENV{PKG_CONFIG_SYSROOT_DIR} "${win32_deps_root}")
set (win32_deps_pcpath
"${win32_deps_prefix}/share/pkgconfig:${win32_deps_prefix}/lib/pkgconfig")
set (ENV{PKG_CONFIG_PATH} "${win32_deps_pcpath}")
set (ENV{PKG_CONFIG_LIBDIR} "${win32_deps_pcpath}")
endif ()
# Dependencies
set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/liberty/cmake)
@ -35,13 +52,14 @@ elseif (APPLE)
add_definitions (-D_DARWIN_C_SOURCE)
endif ()
if (WITH_LIBUSB)
if (WITH_LIBUSB AND NOT WIN32)
list (APPEND targets elksmart-comm)
add_executable (elksmart-comm elksmart-comm.c)
target_include_directories (elksmart-comm PUBLIC ${libusb_INCLUDE_DIRS})
target_link_directories (elksmart-comm PUBLIC ${libusb_LIBRARY_DIRS})
target_link_libraries (elksmart-comm ${libusb_LIBRARIES})
endif ()
if (WITH_LIBUSB)
list (APPEND targets razer-bw-te-ctl)
add_executable (razer-bw-te-ctl razer-bw-te-ctl.c)
target_include_directories (razer-bw-te-ctl PUBLIC ${libusb_INCLUDE_DIRS})
@ -80,27 +98,29 @@ if (WITH_HIDRAW AND WIN32)
target_link_options (eizoctltray PUBLIC -static -municode)
target_include_directories (eizoctltray PUBLIC ${hidapi_INCLUDE_DIRS})
target_link_directories (eizoctltray PUBLIC ${hidapi_LIBRARY_DIRS})
target_link_libraries (eizoctltray ${hidapi_LIBRARIES} PowrProf)
target_link_libraries (eizoctltray ${hidapi_LIBRARIES} powrprof)
endif ()
# Generate documentation from help output
find_program (HELP2MAN_EXECUTABLE help2man)
if (NOT HELP2MAN_EXECUTABLE)
message (FATAL_ERROR "help2man not found")
if (NOT CMAKE_CROSSCOMPILING)
find_program (HELP2MAN_EXECUTABLE help2man)
if (NOT HELP2MAN_EXECUTABLE)
message (FATAL_ERROR "help2man not found")
endif ()
foreach (target ${targets})
set (page_output "${PROJECT_BINARY_DIR}/${target}.1")
list (APPEND project_MAN_PAGES "${page_output}")
add_custom_command (OUTPUT ${page_output}
COMMAND ${HELP2MAN_EXECUTABLE} -N
"${PROJECT_BINARY_DIR}/${target}" -o ${page_output}
DEPENDS ${target}
COMMENT "Generating man page for ${target}" VERBATIM)
endforeach ()
add_custom_target (docs ALL DEPENDS ${project_MAN_PAGES})
endif ()
foreach (target ${targets})
set (page_output "${PROJECT_BINARY_DIR}/${target}.1")
list (APPEND project_MAN_PAGES "${page_output}")
add_custom_command (OUTPUT ${page_output}
COMMAND ${HELP2MAN_EXECUTABLE} -N
"${PROJECT_BINARY_DIR}/${target}" -o ${page_output}
DEPENDS ${target}
COMMENT "Generating man page for ${target}" VERBATIM)
endforeach ()
add_custom_target (docs ALL DEPENDS ${project_MAN_PAGES})
# The files to be installed
include (GNUInstallDirs)

62
Win64Depends.sh Normal file
View File

@ -0,0 +1,62 @@
#!/bin/sh -e
# Win64Depends.sh: download dependencies from MSYS2 for cross-compilation.
# Dependencies: AWK, sed, sha256sum, cURL, bsdtar
repository=https://repo.msys2.org/mingw/mingw64/
status() {
echo "$(tput bold)-- $*$(tput sgr0)"
}
dbsync() {
status Fetching repository DB
[ -f db.tsv ] || curl -# "$repository/mingw64.db" | bsdtar -xOf- | awk '
function flush() { print f["%NAME%"] f["%FILENAME%"] f["%DEPENDS%"] }
NR > 1 && $0 == "%FILENAME%" { flush(); for (i in f) delete f[i] }
!/^[^%]/ { field = $0; next } { f[field] = f[field] $0 "\t" }
field == "%SHA256SUM%" { path = "*packages/" f["%FILENAME%"]
sub(/\t$/, "", path); print $0, path > "db.sums" } END { flush() }
' > db.tsv
}
fetch() {
status Resolving "$@"
mkdir -p packages
awk -F'\t' 'function get(name, i, a) {
if (visited[name]++ || !(name in filenames)) return
print filenames[name]; split(deps[name], a); for (i in a) get(a[i])
} BEGIN { while ((getline < "db.tsv") > 0) {
filenames[$1] = $2; deps[$1] = ""; for (i = 3; i <= NF; i++) {
gsub(/[<=>].*/, "", $i); deps[$1] = deps[$1] $i FS }
} for (i = 0; i < ARGC; i++) get(ARGV[i]) }' "$@" | tee db.want | \
while IFS= read -r name
do
status Fetching "$name"
[ -f "packages/$name" ] || curl -#o "packages/$name" "$repository/$name"
done
}
verify() {
status Verifying checksums
sha256sum --ignore-missing --quiet -c db.sums
}
extract() {
status Extracting packages
for subdir in *
do [ -d "$subdir" -a "$subdir" != packages ] && rm -rf -- "$subdir"
done
while IFS= read -r name
do bsdtar -xf "packages/$name" --strip-components 1
done < db.want
}
# This directory name matches the prefix in .pc files, so we don't need to
# modify them (pkgconf has --prefix-variable, but CMake can't pass that option).
mkdir -p mingw64
cd mingw64
dbsync
fetch mingw-w64-x86_64-hidapi mingw-w64-x86_64-libusb
verify
extract
status Success

View File

@ -309,7 +309,7 @@ apply_options(libusb_device_handle *device,
return 0;
}
#define ERROR(label, ...) \
#define FAIL(label, ...) \
do { \
fprintf(stderr, "Error: " __VA_ARGS__); \
status = 1; \
@ -326,7 +326,7 @@ main(int argc, char *argv[])
int result, status = 0;
if ((result = libusb_init(NULL)))
ERROR(error_0, "libusb initialisation failed: %s\n",
FAIL(error_0, "libusb initialisation failed: %s\n",
libusb_error_name(result));
result = 0;
@ -334,10 +334,10 @@ main(int argc, char *argv[])
find_device(USB_VENDOR_RAZER, USB_PRODUCT_RAZER_BW_TE, &result);
if (!device) {
if (result)
ERROR(error_1, "couldn't open device: %s\n",
FAIL(error_1, "couldn't open device: %s\n",
libusb_error_name(result));
else
ERROR(error_1, "no suitable device found\n");
FAIL(error_1, "no suitable device found\n");
}
bool reattach_driver = false;
@ -348,29 +348,29 @@ main(int argc, char *argv[])
case 1:
reattach_driver = true;
if ((result = libusb_detach_kernel_driver(device, BW_CTL_IFACE)))
ERROR(error_2, "couldn't detach kernel driver: %s\n",
FAIL(error_2, "couldn't detach kernel driver: %s\n",
libusb_error_name(result));
break;
default:
ERROR(error_2, "coudn't detect kernel driver presence: %s\n",
FAIL(error_2, "coudn't detect kernel driver presence: %s\n",
libusb_error_name(result));
}
if ((result = libusb_claim_interface(device, BW_CTL_IFACE)))
ERROR(error_3, "couldn't claim interface: %s\n",
FAIL(error_3, "couldn't claim interface: %s\n",
libusb_error_name(result));
if ((result = apply_options(device, &options, &new_config)))
ERROR(error_4, "operation failed: %s\n",
FAIL(error_4, "operation failed: %s\n",
libusb_error_name (result));
error_4:
if ((result = libusb_release_interface(device, BW_CTL_IFACE)))
ERROR(error_3, "couldn't release interface: %s\n",
FAIL(error_3, "couldn't release interface: %s\n",
libusb_error_name(result));
error_3:
if (reattach_driver) {
if ((result = libusb_attach_kernel_driver(device, BW_CTL_IFACE)))
ERROR(error_2, "couldn't reattach kernel driver: %s\n",
FAIL(error_2, "couldn't reattach kernel driver: %s\n",
libusb_error_name(result));
}