Skip to content

Commit

Permalink
FindRust upgrades, integrate c/bindgen w. build.rs
Browse files Browse the repository at this point in the history
`FindRust.cmake` no longer attempts to guess at the Rust platform target
triple by evaluating the OS, Architecture, and pointer-size of the target except
on Windows (for cross-compiling to x86) and on macOS (for universal binary builds).
Instead, it uses Rust's host target triple obtained with the
command `rustc -vV`.
If you want to cross-compile, you can set the `RUST_COMPILER_TARGET`
variable.

`FindRust.cmake` now sets the Rust build type to Release for CMake's
MinSizeRel and RelWithDebInfo builds, using the `-g` RUSTFLAG for the
latter so that debug symbols are provided.

Renamed a bunch of things to make it more intuitive. Namely the demo
library is now called "demo" instead of "CMakeRust", and the C and Rust
sources are now in separate directories such that the Rust stuff follows
the traditional Rust project layout.

Demo now integrates the bindgen and cbindgen processes into a `build.rs`
file that automatically runs at build time and does not depend on having
bindgen or cbindgen pre-installed. Generating C bindings with cbindgen
works everywhere, but generating the Rust bindings from the C code
appears to require libclang and may not be present on all systems - so
we only run it when MAINTAINER_MODE is ON.

Demo now installs the Rust-built static library.

The demo app is linked with the static libs so we don't have to make
a temporary install on Windows so the app can load the DLL.
Similarly, we link with the static libs for Rust unit test executable, so we
don't need a temporary install to run the test executable.

Also add clang-format spec for convenience in maintaining the C bits.

For Github Actions: switched to using CMake manually.
  • Loading branch information
val-ms authored Mar 21, 2022
1 parent 5f6a78a commit 1a5c950
Show file tree
Hide file tree
Showing 35 changed files with 884 additions and 275 deletions.
148 changes: 148 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: true
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Never
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Linux
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
- Regex: '.*'
Priority: 1
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
...

38 changes: 18 additions & 20 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ name: Build Test
# events but only for the main branch
on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build_and_test:
name: '${{ matrix.os }}: build and test'
name: "${{ matrix.os }}: build and test"
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand All @@ -29,24 +29,23 @@ jobs:
- name: get-cmake
uses: lukka/get-cmake@latest

- name: run-cmake
uses: lukka/[email protected]
with:
cmakeGenerator: 'Ninja'
cmakeListsOrSettingsJson: 'CMakeListsTxtBasic'
buildDirectory: '${{ runner.workspace }}/build/ninja/'
- name: Build project
working-directory: "${{ runner.workspace }}/"
run: |
cmake -B ./build -S ./cmake-rust-demo
cmake --build ./build
- name: Run tests
working-directory: '${{ runner.workspace }}/build/ninja/'
working-directory: "${{ runner.workspace }}/build/"
run: |
ctest -VV
- name: Run the app
run: |
${{ runner.workspace }}/build/ninja/app/app
${{ runner.workspace }}/build/app/app
build_and_test_windows:
name: 'windows-latest: build and test'
name: "windows-latest: build and test"
runs-on: windows-latest

steps:
Expand All @@ -60,18 +59,17 @@ jobs:
- name: get-cmake
uses: lukka/get-cmake@latest

- name: run-cmake
uses: lukka/[email protected]
with:
cmakeGenerator: 'VS16Win64'
cmakeListsOrSettingsJson: 'CMakeListsTxtBasic'
buildDirectory: '${{ runner.workspace }}/build/vs2019/'
- name: Build project
working-directory: "${{ runner.workspace }}/"
run: |
cmake -B ./build -S ./cmake-rust-demo -A x64
cmake --build ./build
- name: Run tests
working-directory: '${{ runner.workspace }}/build/vs2019/'
working-directory: "${{ runner.workspace }}/build/"
run: |
ctest -VV -C Debug
- name: Run the app
run: |
${{ runner.workspace }}\\build\\vs2019\\app\\Debug\\app.exe
${{ runner.workspace }}\\build\\app\\Debug\\app.exe
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ build/*

# Rust / Cargo files
Cargo.lock
target/*
27 changes: 13 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Copyright (C) 2020 Micah Snyder.
# Copyright (C) 2020-2022 Micah Snyder.

cmake_minimum_required(VERSION 3.18)
set(RUSTC_MINIMUM_REQUIRED 1.56)

project( RustCMakeDemo
VERSION "0.1.0"
DESCRIPTION "A demo app to show a CMake project with components written in Rust." )
project(RustCMakeDemo
VERSION "0.2.0"
DESCRIPTION "A demo app to show a CMake project with components written in Rust.")

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

Expand All @@ -16,14 +17,14 @@ option(MAINTAINER_MODE
"Use `cbindgen` to generate Rust library API headers."
${MAINTAINER_MODE_DEFAULT})

if(MAINTAINER_MODE)
set(cbindgen_REQUIRED 1)
endif()
find_package(Rust REQUIRED)

# Always use '-fPIC'/'-fPIE' option.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Include GNUInstallDirs for access to CMAKE_INSTALL_LIBDIR, etc
include(GNUInstallDirs)

# Enable CTest
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
Expand All @@ -44,9 +45,10 @@ include(CPack)
#
# Build targets.
#
add_subdirectory( lib )
add_subdirectory( common )
add_subdirectory( app )
add_subdirectory(lib)
add_subdirectory(common)
add_subdirectory(app)
add_subdirectory(test)

#
# The Summary Info.
Expand All @@ -58,10 +60,7 @@ message(STATUS "Configuration Options Summary --
Build type: ${CMAKE_BUILD_TYPE}
C compiler: ${CMAKE_C_COMPILER}
Rust toolchain: ${cargo_EXECUTABLE} (${cargo_VERSION})
${rustc_EXECUTABLE} (${rustc_VERSION})
CFLAGS: ${CMAKE_C_FLAGS_${_build_type}} ${CMAKE_C_FLAGS}
Build Options:
Maintainer Mode: ${MAINTAINER_MODE}")
if(MAINTAINER_MODE)
message("\
cbindgen: ${cbindgen_EXECUTABLE} (${cbindgen_VERSION})")
endif()
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[workspace]

members = ["lib/rust", "common/gen_uuid"]

[profile.dev.package."*"]
opt-level = 2
Loading

0 comments on commit 1a5c950

Please sign in to comment.