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

Experimental: Cross-compiling with ARM cross-compiler #188

Merged
merged 16 commits into from
Jan 19, 2023

Conversation

abeimler
Copy link
Contributor

  • extend the enable_cross_compiler() for arm triplets
  • add some little arm testing projects (with docker)

I try to experiment with arm cross-compiler and try to figure out what's wrong clang-tidy, cppcheck, etc.

Seems like when I'm building without the static analysis tools, it works.

task docker:rpi3.release

Without static analysis tools

project_options-build-rpi3-release-1  | task: [rpi3:build.release] cmake . -B ./build -DCMAKE_BUILD_TYPE:STRING=Release -G 'Ninja Multi-Config' -DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER=arm-none-eabi-gcc -DCMAKE_CXX_COMPILER=arm-none-eabi-g++ -DDEFAULT_TRIPLET=arm-linux
project_options-build-rpi3-release-1  | -- Unknown architecture  - using x64
project_options-build-rpi3-release-1  | -- enable cross-compiling
project_options-build-rpi3-release-1  | -- Target Architecture: arm-linux
project_options-build-rpi3-release-1  | -- Default Triplet: arm-linux
project_options-build-rpi3-release-1  | -- Host Triplet: x64-linux
project_options-build-rpi3-release-1  | -- Toolchain File: /home/project_options/src/toolchains/arm.toolchain.cmake
project_options-build-rpi3-release-1  | -- The C compiler identification is GNU 10.3.1
project_options-build-rpi3-release-1  | -- The CXX compiler identification is GNU 10.3.1
project_options-build-rpi3-release-1  | -- The ASM compiler identification is GNU
project_options-build-rpi3-release-1  | -- Found assembler: /usr/bin/arm-none-eabi-gcc
project_options-build-rpi3-release-1  | -- Detecting C compiler ABI info
project_options-build-rpi3-release-1  | -- Detecting C compiler ABI info - done
project_options-build-rpi3-release-1  | -- Check for working C compiler: /usr/bin/arm-none-eabi-gcc - skipped
project_options-build-rpi3-release-1  | -- Detecting C compile features
project_options-build-rpi3-release-1  | -- Detecting C compile features - done
project_options-build-rpi3-release-1  | -- Detecting CXX compiler ABI info
project_options-build-rpi3-release-1  | -- Detecting CXX compiler ABI info - done
project_options-build-rpi3-release-1  | -- Check for working CXX compiler: /usr/bin/arm-none-eabi-g++ - skipped
project_options-build-rpi3-release-1  | -- Detecting CXX compile features
project_options-build-rpi3-release-1  | -- Detecting CXX compile features - done
project_options-build-rpi3-release-1  | -- /usr/bin/ccache found and enabled
project_options-build-rpi3-release-1  | -- Configuring done
project_options-build-rpi3-release-1  | -- Generating done
project_options-build-rpi3-release-1  | -- Build files have been written to: /home/project_options/tests/rpi3/build
project_options-build-rpi3-release-1  | task: [rpi3:build.release] cmake --build ./build --config Release
project_options-build-rpi3-release-1  | [1/2] Building C object CMakeFiles/example_c.dir/Release/main.c.o
project_options-build-rpi3-release-1  | cc1: warning: command-line option '-fno-rtti' is valid for C++/D/ObjC++ but not for C
project_options-build-rpi3-release-1  | [2/2] Linking C executable Release/example_c

task docker:rpi4.test

Without static analysis tools

...
project_options-test-rpi4-1  | -- Configuring done
project_options-test-rpi4-1  | -- Generating done
project_options-test-rpi4-1  | -- Build files have been written to: /home/project_options/tests/rpi4/build
project_options-test-rpi4-1  | task: [rpi4:build.release] cmake --build ./build --config Release
project_options-test-rpi4-1  | [1/2] Building CXX object CMakeFiles/example.dir/Release/main.cpp.o
project_options-test-rpi4-1  | [2/2] Linking CXX executable Release/example

task docker:rpi4

With static analysis tools enabled

project_options-build-rpi4-1  | [1/2] Building CXX object CMakeFiles/example.dir/Debug/main.cpp.o
project_options-build-rpi4-1  | FAILED: CMakeFiles/example.dir/Debug/main.cpp.o 
project_options-build-rpi4-1  | /root/cmake/cmake-3.25.0-linux-x86_64/bin/cmake -E __run_co_compile --launcher=/usr/bin/ccache --tidy="/root/llvm/bin/clang-tidy;-extra-arg=-Wno-unknown-warning-option;-extra-arg=-std=c++11;--extra-arg-before=--driver-mode=g++" --cppcheck="/usr/bin/cppcheck;--template=gcc;--enable=style,performance,warning,portability;--inline-suppr;--suppress=cppcheckError;--suppress=internalAstError;--suppress=unmatchedSuppression;--suppress=passedByValue;--suppress=syntaxError;--inconclusive;--std=c++11" --source=/home/project_options/tests/rpi4/main.cpp -- /usr/bin/aarch64-linux-gnu-g++ --sysroot=/usr/gcc-aarch64-linux-gnu -DCMAKE_INTDIR=\"Debug\" -I/home/project_options/tests/rpi4 -g -fdiagnostics-color=always -fno-exceptions -fno-unwind-tables -fno-rtti -mcpu=cortex-a72 -Wall -Wextra -Wextra-semi -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align -Wunused -Woverloaded-virtual -Wpedantic -Wconversion -Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wformat=2 -Wimplicit-fallthrough -Wmisleading-indentation -Wduplicated-cond -Wduplicated-branches -Wlogical-op -Wuseless-cast -std=c++11 -MD -MT CMakeFiles/example.dir/Debug/main.cpp.o -MF CMakeFiles/example.dir/Debug/main.cpp.o.d -o CMakeFiles/example.dir/Debug/main.cpp.o -c /home/project_options/tests/rpi4/main.cpp
project_options-build-rpi4-1  | warning: argument unused during compilation: '-mcpu=cortex-a72' [clang-diagnostic-unused-command-line-argument]
project_options-build-rpi4-1  | /home/project_options/tests/rpi4/main.cpp:1:10: error: 'iostream' file not found [clang-diagnostic-error]
project_options-build-rpi4-1  | #include <iostream>
project_options-build-rpi4-1  |          ^~~~~~~~~~
project_options-build-rpi4-1  | 1 warning and 1 error generated.

Seems like the same issue #180 (comment)

task docker:rpi3

project_options-build-rpi3-1  | FAILED: CMakeFiles/example_c.dir/Debug/main.c.o 
project_options-build-rpi3-1  | /root/cmake/cmake-3.25.0-linux-x86_64/bin/cmake -E __run_co_compile --launcher=/usr/bin/ccache --tidy="/root/llvm/bin/clang-tidy;-extra-arg=-Wno-unknown-warning-option;-extra-arg=-std=c99;--extra-arg-before=--driver-mode=gcc" --cppcheck="/usr/bin/cppcheck;--template=gcc;--enable=style,performance,warning,portability;--inline-suppr;--suppress=cppcheckError;--suppress=internalAstError;--suppress=unmatchedSuppression;--suppress=passedByValue;--suppress=syntaxError;--inconclusive;--std=c99" --source=/home/project_options/tests/rpi3/main.c -- /usr/bin/arm-none-eabi-gcc --sysroot=/usr/gcc-arm-none-eabi -DCMAKE_INTDIR=\"Debug\" -I/home/project_options/tests/rpi3 -g -fdiagnostics-color=always -fno-exceptions -fno-unwind-tables -fno-rtti -march=armv8-a -mfpu=neon-fp-armv8 -mthumb -Wall -Wextra -Wshadow -Wcast-align -Wunused -Wpedantic -Wconversion -Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wformat=2 -Wimplicit-fallthrough -Wmisleading-indentation -Wduplicated-cond -Wduplicated-branches -Wlogical-op -std=c99 -MD -MT CMakeFiles/example_c.dir/Debug/main.c.o -MF CMakeFiles/example_c.dir/Debug/main.c.o.d -o CMakeFiles/example_c.dir/Debug/main.c.o -c /home/project_options/tests/rpi3/main.c
project_options-build-rpi3-1  | error: unknown target CPU 'armv8-a' [clang-diagnostic-error]
project_options-build-rpi3-1  | note: valid target CPU values are: nocona, core2, penryn, bonnell, atom, silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7, westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell, core-avx2, broadwell, skylake, skylake-avx512, skx, cascadelake, cooperlake, cannonlake, icelake-client, rocketlake, icelake-server, tigerlake, sapphirerapids, alderlake, knl, knm, k8, athlon64, athlon-fx, opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10, barcelona, btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, znver2, znver3, x86-64, x86-64-v2, x86-64-v3, x86-64-v4
project_options-build-rpi3-1  | warning: argument unused during compilation: '-mfpu=neon-fp-armv8' [clang-diagnostic-unused-command-line-argument]
project_options-build-rpi3-1  | warning: argument unused during compilation: '-mthumb' [clang-diagnostic-unused-command-line-argument]
project_options-build-rpi3-1  | Error while processing /home/project_options/tests/rpi3/main.c.

Seems like clang-tidy doesn't know it is built with a cross-compiler ? ... (and uses the native clang++ targets.)
@glennvl Maybe we need to add --target for clang-tidy

Maybe this helps...

# The target triple needs to match the prefix of the binutils exactly
# (e.g. CMake looks for arm-none-eabi-ar)
set(CLANG_TARGET_TRIPLE arm-none-eabi)
set(GCC_ARM_TOOLCHAIN_PREFIX ${CLANG_CLANG_TARGET_TRIPLE})
set(CMAKE_C_COMPILER clang)
set(CMAKE_C_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_ASM_COMPILER_TARGET ${CLANG_TARGET_TRIPLE})

https://stackoverflow.com/questions/54539682/how-to-set-up-cmake-to-cross-compile-with-clang-for-arm-embedded-on-windows

task docker:pico

I add this just for fun, may remove it later

project_options-build-pico-1  | /usr/bin/ccache /usr/bin/arm-none-eabi-gcc -DLIB_PICO_BIT_OPS=1 -DLIB_PICO_BIT_OPS_PICO=1 -DLIB_PICO_DIVIDER=1 -DLIB_PICO_DIVIDER_HARDWARE=1 -DLIB_PICO_DOUBLE=1 -DLIB_PICO_DOUBLE_PICO=1 -DLIB_PICO_FLOAT=1 -DLIB_PICO_FLOAT_PICO=1 -DLIB_PICO_INT64_OPS=1 -DLIB_PICO_INT64_OPS_PICO=1 -DLIB_PICO_MALLOC=1 -DLIB_PICO_MEM_OPS=1 -DLIB_PICO_MEM_OPS_PICO=1 -DLIB_PICO_PLATFORM=1 -DLIB_PICO_PRINTF=1 -DLIB_PICO_PRINTF_PICO=1 -DLIB_PICO_RUNTIME=1 -DLIB_PICO_STANDARD_LINK=1 -DLIB_PICO_STDIO=1 -DLIB_PICO_STDIO_UART=1 -DLIB_PICO_STDLIB=1 -DLIB_PICO_SYNC=1 -DLIB_PICO_SYNC_CORE=1 -DLIB_PICO_SYNC_CRITICAL_SECTION=1 -DLIB_PICO_SYNC_MUTEX=1 -DLIB_PICO_SYNC_SEM=1 -DLIB_PICO_TIME=1 -DLIB_PICO_UTIL=1 -DPICO_BOARD=\"pico\" -DPICO_BUILD=1 -DPICO_CMAKE_BUILD_TYPE=\"Release\" -DPICO_COPY_TO_RAM=0 -DPICO_CXX_ENABLE_EXCEPTIONS=0 -DPICO_NO_FLASH=0 -DPICO_NO_HARDWARE=0 -DPICO_ON_DEVICE=1 -DPICO_TARGET_NAME=\"hello_world\" -DPICO_USE_BLOCKED_RAM=0 -DCMAKE_INTDIR=\"Release\" -I/home/pico-sdk/src/common/pico_stdlib/include -I/home/pico-sdk/src/rp2_common/hardware_gpio/include -I/home/pico-sdk/src/common/pico_base/include -I/home/project_options/tests/pico/build/generated/pico_base -I/home/pico-sdk/src/boards/include -I/home/pico-sdk/src/rp2_common/pico_platform/include -I/home/pico-sdk/src/rp2040/hardware_regs/include -I/home/pico-sdk/src/rp2_common/hardware_base/include -I/home/pico-sdk/src/rp2040/hardware_structs/include -I/home/pico-sdk/src/rp2_common/hardware_claim/include -I/home/pico-sdk/src/rp2_common/hardware_sync/include -I/home/pico-sdk/src/rp2_common/hardware_irq/include -I/home/pico-sdk/src/common/pico_sync/include -I/home/pico-sdk/src/common/pico_time/include -I/home/pico-sdk/src/rp2_common/hardware_timer/include -I/home/pico-sdk/src/common/pico_util/include -I/home/pico-sdk/src/rp2_common/hardware_uart/include -I/home/pico-sdk/src/rp2_common/hardware_divider/include -I/home/pico-sdk/src/rp2_common/pico_runtime/include -I/home/pico-sdk/src/rp2_common/hardware_clocks/include -I/home/pico-sdk/src/rp2_common/hardware_resets/include -I/home/pico-sdk/src/rp2_common/hardware_pll/include -I/home/pico-sdk/src/rp2_common/hardware_vreg/include -I/home/pico-sdk/src/rp2_common/hardware_watchdog/include -I/home/pico-sdk/src/rp2_common/hardware_xosc/include -I/home/pico-sdk/src/rp2_common/pico_printf/include -I/home/pico-sdk/src/rp2_common/pico_bootrom/include -I/home/pico-sdk/src/common/pico_bit_ops/include -I/home/pico-sdk/src/common/pico_divider/include -I/home/pico-sdk/src/rp2_common/pico_double/include -I/home/pico-sdk/src/rp2_common/pico_int64_ops/include -I/home/pico-sdk/src/rp2_common/pico_float/include -I/home/pico-sdk/src/rp2_common/pico_malloc/include -I/home/pico-sdk/src/rp2_common/boot_stage2/include -I/home/pico-sdk/src/common/pico_binary_info/include -I/home/pico-sdk/src/rp2_common/pico_stdio/include -I/home/pico-sdk/src/rp2_common/pico_stdio_uart/include -mcpu=cortex-m0plus -mthumb -O3 -DNDEBUG -fdiagnostics-color=always -ffunction-sections -fdata-sections -Wall -Wextra -Wshadow -Wcast-align -Wunused -Wpedantic -Wconversion -Wsign-conversion -Wnull-dereference -Wdouble-promotion -Wformat=2 -Wimplicit-fallthrough -Wmisleading-indentation -Wduplicated-cond -Wduplicated-branches -Wlogical-op -std=c17 -MD -MT CMakeFiles/hello_world.dir/Release/home/pico-sdk/src/common/pico_time/time.c.obj -MF CMakeFiles/hello_world.dir/Release/home/pico-sdk/src/common/pico_time/time.c.obj.d -o CMakeFiles/hello_world.dir/Release/home/pico-sdk/src/common/pico_time/time.c.obj -c /home/pico-sdk/src/common/pico_time/time.c
project_options-build-pico-1  | In file included from /home/pico-sdk/src/common/pico_base/include/pico.h:31,
project_options-build-pico-1  |                  from /home/pico-sdk/src/common/pico_time/time.c:11:
project_options-build-pico-1  | /home/pico-sdk/src/rp2_common/pico_platform/include/pico/platform.h: In function '__mul_instruction':
project_options-build-pico-1  | /home/pico-sdk/src/rp2_common/pico_platform/include/pico/platform.h:365:5: warning: implicit declaration of function 'asm' [-Wimplicit-function-declaration]
project_options-build-pico-1  |   365 |     asm ("mul %0, %1" : "+l" (a) : "l" (b) : );
project_options-build-pico-1  |       |     ^~~
project_options-build-pico-1  | /home/pico-sdk/src/rp2_common/pico_platform/include/pico/platform.h:365:22: error: expected ')' before ':' token
project_options-build-pico-1  |   365 |     asm ("mul %0, %1" : "+l" (a) : "l" (b) : );
project_options-build-pico-1  |       |                      ^~
project_options-build-pico-1  |       |                      )

@aminya aminya marked this pull request as ready for review January 15, 2023 09:39
aminya
aminya previously approved these changes Jan 15, 2023
Copy link
Owner

@aminya aminya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution 🎉

Let me know when this is ready

@abeimler
Copy link
Contributor Author

I added more (vcpkg) triplets:

  • arm64-linux
  • arm-linux

I tried to focus on the "common" arm cross-compilers:

  • gcc-aarch64-linux-gnu
  • gcc-arm-linux-gnueabi
  • gcc-arm-none-eabi

(User still need to setup compiler, triplet and toolchain-file on its own)

-DENABLE_CROSS_COMPILING:BOOL=ON -DCMAKE_C_COMPILER=gcc-aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=gcc-aarch64-linux-gnu-g++ -DDEFAULT_TRIPLET=arm64-linux

@abeimler
Copy link
Contributor Author

I also "accidentally" added CLANG_TIDY_EXTRA_ARGUMENTS, while tryout to fix clang-tidy errors ...

if(ENABLE_CROSS_COMPILING)
  set(CLANG_TIDY_EXTRA_ARGUMENTS --extra-arg=--target=${CROSS_TRIPLET})
endif()
project_options(
  ENABLE_CLANG_TIDY
  CLANG_TIDY_EXTRA_ARGUMENTS
  ${CLANG_TIDY_EXTRA_ARGUMENTS}
  ...)

@abeimler
Copy link
Contributor Author

We still have the problem with (arm) cross-compiler + clang-tidy ... don't know if this needs to be fixed
(I thought this branch can be used for testing different ARM builds and testing for different targets...)

#180 (comment)
#180 (comment)

IMHO I don't really need that feature for cross-compiling, I compiler and unit-tests on my Desktop + IDE + analyzer ON, etc. ... and later deploy as Releasse-Build on the target device. (and do integration tests..)

@abeimler
Copy link
Contributor Author

  • Test cross-compiling without using Conan NOR vcpkg, just plain C++
    • by just using enable_cross_compiler() and CMAKE_TOOLCHAIN_FILE

#180 (comment)

This is also tested und can be marked as DONE by running task docker:rpi4.custom

@aminya aminya closed this Jan 17, 2023
@aminya aminya reopened this Jan 17, 2023
@aminya aminya mentioned this pull request Jan 17, 2023
9 tasks
@glennvl
Copy link

glennvl commented Jan 17, 2023

First of all, thanks for looking into arm cross-compilation

We still have the problem with (arm) cross-compiler + clang-tidy ... don't know if this needs to be fixed (I thought this branch can be used for testing different ARM builds and testing for different targets...)

#180 (comment) #180 (comment)

IMHO I don't really need that feature for cross-compiling, I compiler and unit-tests on my Desktop + IDE + analyzer ON, etc. ... and later deploy as Releasse-Build on the target device. (and do integration tests..)

I don't entirely agree with that argument. While the vast majority of the code will/should be covered by unittests, there will always be low level stuff that won't (such as startup code, memory mapped objects for peripherals, ...). It would be nice to have static analysis cover them, since they are also the parts that are in my opinion most susceptible to accidental undefined behaviour.

* add git submodule
@abeimler
Copy link
Contributor Author

While the docker builds seems to work, the CI are not :( ...

@abeimler
Copy link
Contributor Author

@aminya I updated to the new setup-cpp version 👍
I may found an bug ...

npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/ci-log - Not found
npm ERR! 404
npm ERR! 404  'ci-log@1.0.0' is not in this registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2023-01-18T09_11_37_711Z-debug-0.log

https://www.npmjs.com/package/ci-log ... "package 'ci-log' not found"

@aminya
Copy link
Owner

aminya commented Jan 18, 2023

Oops. I am fixing it in aminya/setup-cpp#160

@aminya
Copy link
Owner

aminya commented Jan 18, 2023

Released a new version. The issue should be fixed now.

* use awalsh128/cache-apt-pkgs-action
@abeimler
Copy link
Contributor Author

First of all, thanks for looking into arm cross-compilation

We still have the problem with (arm) cross-compiler + clang-tidy ... don't know if this needs to be fixed (I thought this branch can be used for testing different ARM builds and testing for different targets...)
#180 (comment) #180 (comment)
IMHO I don't really need that feature for cross-compiling, I compiler and unit-tests on my Desktop + IDE + analyzer ON, etc. ... and later deploy as Releasse-Build on the target device. (and do integration tests..)

I don't entirely agree with that argument. While the vast majority of the code will/should be covered by unittests, there will always be low level stuff that won't (such as startup code, memory mapped objects for peripherals, ...). It would be nice to have static analysis cover them, since they are also the parts that are in my opinion most susceptible to accidental undefined behaviour.

If you want, you can experiment with this branch and try to add this into your project und give us some feedback ...

Better yet ... make an issue with the some details you already writen down in the other issue

clang-tidy complains about unused compiler arguments

Related to this discussion https://discourse.cmake.org/t/using-clang-tidy-with-cross-compilation/5435

I am attempting to use project_options v0.26.3 for an embedded project. The idea is to write everything from scratch in C++, but as a first step I'm attempting to get the build system and tooling setup, for which I'm using the devkit demo code which is written in C. I am using vscode on windows with ubuntu in wsl (after I get everything to work I want to use docker).

What I noticed so far (when cross compiling for the target board) is:

* `clang-tidy` complains about unused compiler arguments

...

(this seems to be an issue on it's own ... I may want to test this issue with the mingw cross-compiler)

@abeimler abeimler requested a review from aminya January 18, 2023 10:20
Copy link
Owner

@aminya aminya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 🎉

@aminya aminya merged commit 655154d into aminya:main Jan 19, 2023
@aminya aminya linked an issue Aug 22, 2023 that may be closed by this pull request
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cross-compiling
3 participants