From af348ea8c20a8dd0c72aa61c6eba34b046c2340b Mon Sep 17 00:00:00 2001 From: Kent McLeod Date: Wed, 25 Jul 2018 16:07:58 +1000 Subject: [PATCH] CMake: Add rust helpers and x86_64 custom target Added helper function to build a rust library using xargo and a custom target x86_64-sel4-none. --- cmake-tool/common.cmake | 1 + cmake-tool/helpers/rust.cmake | 76 +++++++++++++++++++ cmake-tool/rust_targets/x86_64-sel4-none.json | 29 +++++++ 3 files changed, 106 insertions(+) create mode 100644 cmake-tool/helpers/rust.cmake create mode 100644 cmake-tool/rust_targets/x86_64-sel4-none.json diff --git a/cmake-tool/common.cmake b/cmake-tool/common.cmake index a1bc9846..bd6ca3b1 100644 --- a/cmake-tool/common.cmake +++ b/cmake-tool/common.cmake @@ -16,6 +16,7 @@ include("${CMAKE_CURRENT_LIST_DIR}/helpers/application_settings.cmake") include("${CMAKE_CURRENT_LIST_DIR}/helpers/cakeml.cmake") include("${CMAKE_CURRENT_LIST_DIR}/helpers/cross_compiling.cmake") include("${CMAKE_CURRENT_LIST_DIR}/helpers/external-project-helpers.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/helpers/rust.cmake") # Helper function for modifying the linker flags of a target to set the entry point as _sel4_start function(SetSeL4Start target) set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -u _sel4_start -e _sel4_start ") diff --git a/cmake-tool/helpers/rust.cmake b/cmake-tool/helpers/rust.cmake new file mode 100644 index 00000000..dcd7be1f --- /dev/null +++ b/cmake-tool/helpers/rust.cmake @@ -0,0 +1,76 @@ +# +# Copyright 2018, Data61 +# Commonwealth Scientific and Industrial Research Organisation (CSIRO) +# ABN 41 687 119 230. +# +# This software may be distributed and modified according to the terms of +# the BSD 2-Clause license. Note that NO WARRANTY is provided. +# See "LICENSE_BSD2.txt" for details. +# +# @TAG(DATA61_BSD) +# + +cmake_minimum_required(VERSION 3.8.2) + + +set(rust_target_dir ${CMAKE_CURRENT_LIST_DIR}/../rust_targets) +# Return absoulute filepath of the rust custom target directory +function(RustGetCustomTargetPath return) + set(${return} ${rust_target_dir} PARENT_SCOPE) +endfunction() + + +# add_library but for rust libraries. Invokes cargo in the SOURCE_DIR that is provided, +# all build output is placed in BUILD_DIR or CMAKE_CURRENT_BINARY_DIR if BUILD_DIR isn't provided. +# lib_name: Name of library that is created +# SOURCE_DIR: source directory of cargo project +# BUILD_DIR: directory for cargo build output +# TARGET: custom target to use. See in ../rust_targets/ for list of available targets. +# LIB_FILENAME: filename of library created by cargo +# DEPENDS: And target or file dependencies that need to be run before cargo +function(RustAddLibrary lib_name) + cmake_parse_arguments(PARSE_ARGV 1 RUST + "" + "SOURCE_DIR;BUILD_DIR;TARGET;LIB_FILENAME" + "DEPENDS" + ) + if (NOT "${RUST_UNPARSED_ARGUMENTS}" STREQUAL "") + message(FATAL_ERROR "Unknown arguments to RustAddLibrary ${RUST_UNPARSED_ARGUMENTS}") + endif() + if ("${RUST_SOURCE_DIR}" STREQUAL "") + message(FATAL_ERROR "SOURCE_DIR must be set for RustAddLibrary") + endif() + if ("${RUST_LIB_FILENAME}" STREQUAL "") + message(FATAL_ERROR "LIB_FILENAME must be set for RustAddLibrary") + endif() + if ("${RUST_BUILD_DIR}" STREQUAL "") + set(RUST_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}) + endif() + RustGetCustomTargetPath(target_path) + if ("${RUST_TARGET}" STREQUAL "") + message(FATAL_ERROR "TARGET must be set for RustAddLibrary. Valid options can be found in: ${target_path}") + endif() + string(FIND "${RUST_TARGET}" ".json" pos) + if (NOT ${pos} EQUAL -1) + message(FATAL_ERROR "${RUST_TARGET} cannot contain .json") + endif() + + find_program(XARGO_TOOL xargo) + if ("${XARGO_TOOL}" STREQUAL "XARGO_TOOL-NOTFOUND") + message(FATAL_ERROR "Could not find tool xargo. Install with `cargo install xargo` \ + or see https://github.com/japaric/xargo") + endif() + + add_custom_target(${libmain}_custom + BYPRODUCTS ${RUST_BUILD_DIR}/${RUST_LIB_FILENAME} + USES_TERMINAL + DEPENDS ${RUST_DEPENDS} + WORKING_DIRECTORY ${RUST_SOURCE_DIR} + COMMAND ${CMAKE_COMMAND} -E env RUST_TARGET_PATH=${target_path} + ${XARGO_TOOL} build --target ${RUST_TARGET} --target-dir ${RUST_BUILD_DIR} -Z unstable-options --out-dir ${RUST_BUILD_DIR} + ) + + add_library(${lib_name} STATIC IMPORTED GLOBAL) + set_property(TARGET ${lib_name} PROPERTY IMPORTED_LOCATION "${RUST_BUILD_DIR}/${RUST_LIB_FILENAME}") + add_dependencies(${lib_name} ${libmain}_custom) +endfunction() diff --git a/cmake-tool/rust_targets/x86_64-sel4-none.json b/cmake-tool/rust_targets/x86_64-sel4-none.json new file mode 100644 index 00000000..00000cb6 --- /dev/null +++ b/cmake-tool/rust_targets/x86_64-sel4-none.json @@ -0,0 +1,29 @@ +{ + "arch": "x86_64", + "os": "none", + "llvm-target": "x86_64-elf", + "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", + "target-pointer-width": "64", + "target-c-int-width": "64", + "cpu" : "westmere", + "target-endian": "little", + "env": "none", + "pre-link-args" : [ + "-m64", "-ffreestanding", "-nodefaultlibs", "-nostdlib", "-fno-stack-protector" + ], + "post-link-args" : [ + "-lgcc", "-lgcc_eh", "-lnosys", "-lm" + ], + "disable-redzone": false, + "executables": true, + "relocation-model": "pic", + "has-elf-tls": false, + "linker-is-gnu": true, + "relocation-model": "static", + "position-independent-executables": false, + "dynamic_linking": false, + "panic-strategy": "abort", + "no-default-libraries": true, + "no-compiler-rt": false, + "linker-flavor": "gcc" +}