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

test if test catches error #39

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/actions/setup_test/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ runs:
"${{steps.arch_flags.outputs.cmake}}"
"${{steps.pick_compiler.outputs.c_compiler}}"
"${{steps.pick_compiler.outputs.cxx_compiler}}"
${{ steps.pick_compiler.outputs.c_compiler_target }}
${{ steps.pick_compiler.outputs.cxx_compiler_target }}
"${{steps.determine_cross_compile.outputs.system_name}}"
"${{steps.pick_generator.outputs.generator}}"
${{steps.install_prefix.outputs.install_path}}
Expand Down
10 changes: 8 additions & 2 deletions .github/scripts/determine_compiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ echo "Compiler Family: '${compiler_kind}'"
if [[ "${compiler_kind}" == "clang" ]]; then
c_compiler="clang"
cxx_compiler="clang++"
if [[ -n "${target_system_name}" ]]; then
# In CI we only cross-compile to linux for now
compiler_target="${target_arch}-linux-gnu"
echo "c_compiler_target=-DCMAKE_C_COMPILER_TARGET=${compiler_target}" >> "$GITHUB_OUTPUT"
echo "cxx_compiler_target=-DCMAKE_CXX_COMPILER_TARGET=${compiler_target}" >> "$GITHUB_OUTPUT"
fi
elif [[ "${compiler_kind}" == "msvc" ]]; then
c_compiler="cl"
cxx_compiler="cl"
Expand All @@ -55,5 +61,5 @@ elif [[ "${compiler_kind}" == "gcc" ]]; then
fi
echo "Chose C compiler: '${c_compiler}'"
echo "Chose C++ compiler: '${cxx_compiler}'"
echo "c_compiler=-DCMAKE_C_COMPILER=${c_compiler}" >> $GITHUB_OUTPUT
echo "cxx_compiler=-DCMAKE_CXX_COMPILER=${cxx_compiler}" >> $GITHUB_OUTPUT
echo "c_compiler=-DCMAKE_C_COMPILER=${c_compiler}" >> "$GITHUB_OUTPUT"
echo "cxx_compiler=-DCMAKE_CXX_COMPILER=${cxx_compiler}" >> "$GITHUB_OUTPUT"
28 changes: 28 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ jobs:
os: windows-2019
rust: 1.46.0

test_legacy_stable:
name: Legacy CMake + stable Rust
uses: ./.github/workflows/test_legacy.yaml
strategy:
fail-fast: false
matrix:
os:
- windows-2019 # windows-latest is currently not having a supported MSVC compiler
- ubuntu-20.04
- macos-12
with:
os: ${{ matrix.os }}
rust: stable

test:
name: Test Corrosion
Expand Down Expand Up @@ -90,6 +103,20 @@ jobs:
cmake: 3.20.0
rust: 1.54
generator: ninja-multiconfig
# Test cross-compiling with clang and bash script
- os: ubuntu-latest
arch: aarch64
abi: gnu
cmake: 3.20.6
rust: 1.68.1
compiler: clang
# Test cross-compiling with clang and rust wrapper
- os: ubuntu-latest
arch: aarch64
abi: gnu
cmake: 3.15.7
rust: 1.68.1
compiler: clang

exclude:

Expand Down Expand Up @@ -305,6 +332,7 @@ jobs:
- test_legacy_linux
- test_legacy_mac
- test_legacy_windows
- test_legacy_stable
- test
- test_msvc
- test_cxxbridge
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ project(Corrosion
HOMEPAGE_URL "https://corrosion-rs.github.io/corrosion/"
)

message(STATUS "Main project CMAKE_C_COMPILER_TARGET: ${CMAKE_C_COMPILER_TARGET}")

# Default behavior:
# - If the project is being used as a subdirectory, then don't build tests and
# don't enable any languages.
Expand Down
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Unreleased

### Fixes

- Fix the PROFILE option with CMake < 3.19 [#427]

# 0.4.2 (2023-07-16)

### Fixes
Expand Down
150 changes: 141 additions & 9 deletions cmake/Corrosion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,129 @@ else()
set(_CORR_PROP_HOST_BUILD INTERFACE_CORROSION_USE_HOST_BUILD CACHE INTERNAL "")
endif()

function(_corrosion_create_linker_wrapper lang out_compiler_wrapper_path)
if(NOT lang MATCHES "^(C|CXX)$")
message(FATAL_ERROR "Internal corrosion error in _corrosion_create_linker_wrapper: lang was ${lang}")
endif()
set(COMPILER_DRIVER "${CMAKE_${lang}_COMPILER}")
set(COMPILER_TARGET "${CMAKE_${lang}_COMPILER_TARGET}")
message(STATUS "Creating linker wrapper script to fix cross-linking with `${COMPILER_DRIVER}` for target `${COMPILER_TARGET}`")
set("bash_wrapper_template"
[==[#!/usr/bin/env bash

@COMPILER_DRIVER@ --target=@COMPILER_TARGET@ "${@}"
]==]
)
set(rust_wrapper_template
[==[
use std::process::Command;

fn main() {
let mut args = std::env::args_os();
// Strip the first arg (name of self) so we get the same as "${@}" with bash
let _our_name = args.next().expect("Corrosion linker driver called with no arguments");
let mut handle = Command::new("@COMPILER_DRIVER@")
.arg("--target=@COMPILER_TARGET@")
.args(args)
.spawn()
.expect("Failed to spawn");
let res = handle.wait().expect("Command wasn't running");
let exit_code = res.code().unwrap_or(127);
std::process::exit(exit_code);
}

]==]
)
set(rust_cargo_toml_template
[==[[package]
name = "wrapper"
version = "0.1.0"
edition = "2018"

[[bin]]
name = "compiler-driver"
path = "main.rs"
]==]
)
set(compiler_driver_dir "${CMAKE_CURRENT_BINARY_DIR}/corrosion/compiler_driver/${lang}")
file(MAKE_DIRECTORY "${compiler_driver_dir}")
# rustc linker-flavor inference parses the linker driver name, so we use the same name as the original
get_filename_component(COMPILER_NAME "${COMPILER_DRIVER}" NAME)

if(CMAKE_HOST_WIN32 OR (CMAKE_VERSION VERSION_LESS "3.20"))
set(compiler_driver_main_rs "${compiler_driver_dir}/main.rs")
set(compiler_driver_cargo_toml "${compiler_driver_dir}/Cargo.toml")

file(WRITE "${compiler_driver_main_rs}.in" "${rust_wrapper_template}")
file(WRITE "${compiler_driver_cargo_toml}.in" "${rust_cargo_toml_template}")
configure_file("${compiler_driver_main_rs}.in" "${compiler_driver_main_rs}" @ONLY)
configure_file("${compiler_driver_cargo_toml}.in" "${compiler_driver_cargo_toml}" @ONLY)

# The name in the Cargo.toml may not contain certain characters, e.g. `clang++` is not valid.
# As a workaround we copy to the required filename after building.
set(compiler_driver_artifact_path "${compiler_driver_dir}/target/release/compiler-driver")
set(compiler_driver_path "${compiler_driver_dir}/${COMPILER_NAME}")
execute_process(
COMMAND ${CMAKE_COMMAND} -E env
"CARGO_BUILD_RUSTC=${_CORROSION_RUSTC}"
"${_CORROSION_CARGO}"
build --release
COMMAND_ECHO "STDOUT"
# OUTPUT_FILE "${compiler_driver_dir}/build_stdout.log"
# ERROR_FILE "${compiler_driver_dir}/build_stderr.log"
WORKING_DIRECTORY "${compiler_driver_dir}"
RESULT_VARIABLE build_compiler_wrapper_res
)
if(NOT build_compiler_wrapper_res EQUAL "0")
message(FATAL_ERROR "Failed to compile linker wrapper script in ${compiler_driver_dir} with ${build_compiler_wrapper_res}")
endif()
execute_process(
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${compiler_driver_artifact_path}" "${compiler_driver_path}"
COMMAND_ECHO "STDOUT"
# OUTPUT_FILE "${compiler_driver_dir}/build_stdout.log"
# ERROR_FILE "${compiler_driver_dir}/build_stderr.log"
WORKING_DIRECTORY "${compiler_driver_dir}"
RESULT_VARIABLE build_compiler_wrapper_res
)
if(NOT build_compiler_wrapper_res EQUAL "0")
message(FATAL_ERROR "Failed to copy linker driver to ${compiler_driver_path}: ${build_compiler_wrapper_res}")
endif()
if(CMAKE_HOST_WIN32)
set(compiler_driver_path "${compiler_driver_path}.exe")
endif()
else()
# requires CMake 3.20

set(compiler_driver_path "${compiler_driver_dir}/${COMPILER_NAME}")
file(CONFIGURE OUTPUT "${compiler_driver_path}"
CONTENT "${bash_wrapper_template}"
@ONLY
)
file(CHMOD "${compiler_driver_path}" "${compiler_driver_dir}"
FILE_PERMISSIONS
"OWNER_READ"
"OWNER_WRITE"
"OWNER_EXECUTE"
"GROUP_READ"
"GROUP_EXECUTE"
"WORLD_READ"
"WORLD_EXECUTE"
)
endif()
set("${out_compiler_wrapper_path}" "${compiler_driver_path}" PARENT_SCOPE)
endfunction()

if(Rust_CROSSCOMPILING AND (CMAKE_C_COMPILER_TARGET OR CMAKE_CXX_COMPILER_TARGET))
if(CMAKE_C_COMPILER_TARGET)
_corrosion_create_linker_wrapper(C c_linker_driver)
set(_CORR_CROSS_C_LINKER_DRIVER "${c_linker_driver}" CACHE INTERNAL "" FORCE)
endif()
if(CMAKE_CXX_COMPILER_TARGET)
_corrosion_create_linker_wrapper(CXX cxx_linker_driver)
set(_CORR_CROSS_CXX_LINKER_DRIVER "${cxx_linker_driver}" CACHE INTERNAL "" FORCE)
endif()
endif()

# Add custom command to build one target in a package (crate)
#
# A target may be either a specific bin
Expand Down Expand Up @@ -851,19 +974,30 @@ function(_add_cargo_build out_cargo_build_out_dir)
set(cargo_target_linker $<$<BOOL:${linker}>:${cargo_target_linker_var}=${linker}>)

if(Rust_CROSSCOMPILING AND (CMAKE_C_COMPILER_TARGET OR CMAKE_CXX_COMPILER_TARGET))
set(linker_target_triple "$<IF:$<BOOL:${target_uses_cxx}>,${CMAKE_CXX_COMPILER_TARGET},${CMAKE_C_COMPILER_TARGET}>")
set(rustflag_linker_arg "-Clink-args=--target=${linker_target_triple}")
set(rustflag_linker_arg "$<${if_not_host_build_condition}:${rustflag_linker_arg}>")
# Skip adding the linker argument, if the linker is explicitly set, since the
# explicit_linker_property will not be set when this function runs.
# Passing this rustflag is necessary for clang.
corrosion_add_target_local_rustflags("${target_name}" "$<$<NOT:${explicit_linker_defined}>:${rustflag_linker_arg}>")
set(cross_linker "$<IF:$<BOOL:${target_uses_cxx}>,${_CORR_CROSS_CXX_LINKER_DRIVER},${_CORR_CROSS_C_LINKER_DRIVER}>")
set(cross_linker "$<${if_not_host_build_condition}:${cross_linker}>")
set(linker "$<IF:${explicit_linker_defined},${explicit_linker_property},${cross_linker}>")
set(cargo_target_linker $<$<BOOL:${linker}>:${cargo_target_linker_var}=${linker}>)
else()
message(STATUS "Not adding CC args")
endif()

message(DEBUG "TARGET ${target_name} produces byproducts ${byproducts}")

add_custom_target(
_cargo-build_${target_name}
COMMAND
${CMAKE_COMMAND} -E echo
"Environment variables..."
"${build_env_variable_genex}"
"${global_rustflags_genex}"
"${cargo_target_linker}"
"${corrosion_cc_rs_flags}"
"${cargo_library_path}"
COMMAND
${CMAKE_COMMAND} -E echo
"local rustflags: ${local_rustflags_genex}"

# Build crate
COMMAND
${CMAKE_COMMAND} -E env
Expand Down Expand Up @@ -1022,8 +1156,6 @@ function(corrosion_import_crate)
" cannot predict. Please consider using a custom cargo profile which inherits from the"
" built-in profile instead."
)
else()
set(cargo_profile_native_generator --profile=${COR_PROFILE})
endif()
endif()

Expand Down
13 changes: 13 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ if(NOT CORROSION_TESTS)
return()
endif()

message(STATUS "Main project tests CMAKE_C_COMPILER_TARGET: ${CMAKE_C_COMPILER_TARGET}")


option(CORROSION_TESTS_CXXBRIDGE
"Build cxxbridge tests which requires cxxbridge executable being available"
OFF)
Expand Down Expand Up @@ -87,13 +90,21 @@ function(corrosion_tests_add_test test_name bin_names)
set(test_dir "${test_name}")
endif()

message(STATUS "corrosion_tests_add_test CMAKE_C_COMPILER_TARGET: ${CMAKE_C_COMPILER_TARGET}")

if(CMAKE_C_COMPILER)
set(TEST_C_COMPILER "C_COMPILER" "${CMAKE_C_COMPILER}")
endif()
if(CMAKE_CXX_COMPILER)
set(TEST_CXX_COMPILER "CXX_COMPILER" "${CMAKE_CXX_COMPILER}")
endif()
if(CMAKE_C_COMPILER_TARGET)
set(TEST_C_COMPILER_TARGET "C_COMPILER_TARGET" "${CMAKE_C_COMPILER_TARGET}")
endif()
if(CMAKE_CXX_COMPILER_TARGET)
set(TEST_CXX_COMPILER_TARGET "CXX_COMPILER_TARGET" "${CMAKE_CXX_COMPILER_TARGET}")
endif()
message(STATUS "before add test: TEST_C_COMPILER_TARGET: ${TEST_C_COMPILER_TARGET}")
if(CMAKE_GENERATOR_PLATFORM)
set(TEST_GENERATOR_PLATFORM "GENERATOR_PLATFORM" "${CMAKE_GENERATOR_PLATFORM}")
endif()
Expand All @@ -117,6 +128,8 @@ function(corrosion_tests_add_test test_name bin_names)
"${TEST_SYSTEM_NAME}"
"${TEST_C_COMPILER}"
"${TEST_CXX_COMPILER}"
"${TEST_C_COMPILER_TARGET}"
"${TEST_CXX_COMPILER_TARGET}"
"${TEST_GENERATOR_PLATFORM}"
"${TEST_GENERATOR_BIN}"
${pass_through_arguments}
Expand Down
16 changes: 15 additions & 1 deletion test/ConfigureAndBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ set(oneValueArgs
C_COMPILER
CXX_COMPILER
SYSTEM_NAME
C_COMPILER_TARGET
CXX_COMPILER_TARGET
EXTERNAL_CORROSION_GENERATOR
CARGO_PROFILE
)
set(multiValueArgs "PASS_THROUGH_ARGS")
cmake_parse_arguments(TEST "${options}" "${oneValueArgs}"
"${multiValueArgs}" ${TEST_ARG_LIST} )

message(STATUS "ConfigureAndBuild unparsed arguments: ${TEST_UNPARSED_ARGUMENT}")
if(TEST_CARGO_TARGET)
set(TEST_Rust_CARGO_TARGET "-DRust_CARGO_TARGET=${TEST_CARGO_TARGET}")
endif()
Expand All @@ -40,6 +43,14 @@ endif()
if(TEST_CXX_COMPILER)
set(TEST_CXX_COMPILER "-DCMAKE_CXX_COMPILER=${TEST_CXX_COMPILER}")
endif()
message(STATUS "TEST_C_COMPILER_TARGET before if: ${TEST_C_COMPILER_TARGET}, cxx: ${TEST_CXX_COMPILER_TARGET}")
if(TEST_C_COMPILER_TARGET)
set(TEST_C_COMPILER_TARGET "-DCMAKE_C_COMPILER_TARGET=${TEST_C_COMPILER_TARGET}")
endif()
if(TEST_CXX_COMPILER_TARGET)
set(TEST_CXX_COMPILER_TARGET "-DCMAKE_CXX_COMPILER_TARGET=${TEST_CXX_COMPILER_TARGET}")
endif()
message(STATUS "TEST_C_COMPILER_TARGET: ${TEST_C_COMPILER_TARGET}")
if(TEST_SYSTEM_NAME)
set(TEST_SYSTEM_NAME "-DCMAKE_SYSTEM_NAME=${TEST_SYSTEM_NAME}")
endif()
Expand Down Expand Up @@ -70,6 +81,8 @@ execute_process(
${TEST_GENERATOR_PLATFORM}
${TEST_C_COMPILER}
${TEST_CXX_COMPILER}
${TEST_C_COMPILER_TARGET}
${TEST_CXX_COMPILER_TARGET}
${TEST_SYSTEM_NAME}
${TEST_EXTERNAL_CORROSION_GENERATOR}
${TEST_CARGO_PROFILE}
Expand All @@ -92,6 +105,7 @@ if ("${TEST_GENERATOR}" STREQUAL "Ninja Multi-Config"
COMMAND "${CMAKE_COMMAND}"
--build "${TEST_BINARY_DIR}"
--config "${config}"
--verbose
COMMAND_ECHO STDOUT
RESULT_VARIABLE EXIT_CODE
)
Expand All @@ -102,7 +116,7 @@ if ("${TEST_GENERATOR}" STREQUAL "Ninja Multi-Config"
endforeach()
else()
execute_process(
COMMAND "${CMAKE_COMMAND}" --build "${TEST_BINARY_DIR}"
COMMAND "${CMAKE_COMMAND}" --build "${TEST_BINARY_DIR}" --verbose
COMMAND_ECHO STDOUT
RESULT_VARIABLE EXIT_CODE
)
Expand Down
1 change: 1 addition & 0 deletions test/multitarget/multitarget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ set_property(TARGET cpp-lib4 PROPERTY POSITION_INDEPENDENT_CODE ON)
corrosion_link_libraries(bin1 cpp-lib4)
corrosion_link_libraries(bin2 cpp-lib4)
corrosion_link_libraries(bin3 cpp-lib4)
corrosion_link_libraries(multitarget_lib cpp-lib4)
Loading