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

Add Tricore options to arch list #16317

Draft
wants to merge 4 commits into
base: develop2
Choose a base branch
from

Conversation

photex
Copy link

@photex photex commented May 22, 2024

Changelog: Feature: Add tricore compiler architecture support
Docs: TODO

Close #16318

Hello! I'd like to start the process here in adding support (whatever that might mean) for tricore-gcc. Currently I'm not able to use the settings.arch to specify the actual arch I'm using, and I assume this probably goes a bit deeper than just updating the yaml file.

For the most part I'm building my firmware by hook and by crook without this support. But It would be nice to learn how to do things properly.

Unlike some other proprietary toolchains that target Aurix devices, tricore-gcc can be built yourself: https://github.com/EEESlab/tricore-gcc-toolchain-11.3.0

I already have a Conan package for this which I'm using, but it's held together with duct-tape and wishful thinking still.

Changelog: Feature: Describe here your pull request
Docs: https://github.com/conan-io/docs/pull/XXXX

@CLAassistant
Copy link

CLAassistant commented May 22, 2024

CLA assistant check
All committers have signed the CLA.

@memsharded
Copy link
Member

Thanks for your contribution @photex

You are right, adding the settings is the first thing. But I also think that it would be good to have at least a check that it works for at least the basic CMake integration:

  • we can assume the compiler is in the system, no need to have it in a conan package
  • the basic conan new cmake_lib better if used with a dependency like zlib/1.3 should be able to compile with the new settings

@photex
Copy link
Author

photex commented May 22, 2024

Is there another common library used for testing baremetal toolchains? I haven't tried zlib yet, but Aurix (tricore) is an embedded platform and I don't know if zlib is expected to work there. I'll give it a shot regardless.

@memsharded
Copy link
Member

Is there another common library used for testing baremetal toolchains? I haven't tried zlib yet, but Aurix (tricore) is an embedded platform and I don't know if zlib is expected to work there. I'll give it a shot regardless.

Not really, actually zlib is kind of an ubiquitous library that is likely to be buildable in most of the platforms, and not affected much by architectures or other platform specifics, this is why it is a good candidate for testing things.

@photex
Copy link
Author

photex commented May 23, 2024

Alrighty, I'm able to build zlib using one of the tricore archs:

Profile used:

[settings]
os=baremetal
arch=tc162
compiler=gcc
compiler.version=11
compiler.cppstd=20
compiler.libcxx=libstdc++11

[options]
# shared=True option only for zlib package
zlib/*:shared=False
zlib/*:fPIC=False

[conf]
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
tools.build:compiler_executables={"c":"tricore-elf-gcc","cpp":"tricore-elf-g++"}

[buildenv]
CMAKE_TRY_COMPILE_TARGET_TYPE="STATIC_LIBRARY"
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM="NEVER"
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY="ONLY"
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE="ONLY"
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE="ONLY"
CMAKE_SYSTEM_NAME="baremetal"
CMAKE_SYSTEM_PROCESSOR="tricore"
CMAKE_C_FLAGS="-mtc162"
CMAKE_CXX_FLAGS="-mtc162"
CMAKE_LINKER_FLAGS="-mtc162"

Command line used:

conan install . -pr:h ~aurix_tc377tx -of build -s build_type=Release --build="*"

Output:

(.venv) [chipc@bucket foo]$ conan install . -pr:h ~/workspaces/kvs/profiles/aurix_tc377tx -of build -s build_type=Release --build="*"

======== Input profiles ========
Profile host:
[settings]
arch=tc162
build_type=Release
compiler=gcc
compiler.cppstd=20
compiler.libcxx=libstdc++11
compiler.version=11
os=baremetal
[options]
zlib/*:fPIC=False
zlib/*:shared=False
[conf]
tools.build:compiler_executables={'c': 'tricore-elf-gcc', 'cpp': 'tricore-elf-g++'}
tools.cmake.cmaketoolchain:generator=Ninja Multi-Config
[buildenv]
CMAKE_TRY_COMPILE_TARGET_TYPE="STATIC_LIBRARY"
CMAKE_FIND_ROOT_PATH_MODE_PROGRAM="NEVER"
CMAKE_FIND_ROOT_PATH_MODE_LIBRARY="ONLY"
CMAKE_FIND_ROOT_PATH_MODE_INCLUDE="ONLY"
CMAKE_FIND_ROOT_PATH_MODE_PACKAGE="ONLY"
CMAKE_SYSTEM_NAME="baremetal"
CMAKE_SYSTEM_PROCESSOR="tricore"
CMAKE_C_FLAGS="-mtc162"
CMAKE_CXX_FLAGS="-mtc162"
CMAKE_LINKER_FLAGS="-mtc162"

Profile build:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=11
os=Linux


======== Computing dependency graph ========
Graph root
    conanfile.py (foo/1.0): /home/chipc/workspaces/conan/foo/conanfile.py
Requirements
    zlib/1.2.13#4e74ebf1361fe6fb60326f473f276eb5 - Cache

======== Computing necessary packages ========
zlib/1.2.13: Forced build from source
Requirements
    zlib/1.2.13#4e74ebf1361fe6fb60326f473f276eb5:09b09ac0f270c2f8bb2a6716c0e57b1983c1682d - Build

======== Installing packages ========

-------- Installing package zlib/1.2.13 (1 of 1) --------
zlib/1.2.13: Building from source
zlib/1.2.13: Package zlib/1.2.13:09b09ac0f270c2f8bb2a6716c0e57b1983c1682d
zlib/1.2.13: Copying sources to build folder
zlib/1.2.13: Building your package in /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b
zlib/1.2.13: Calling generate()
zlib/1.2.13: Generators folder: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build/generators
zlib/1.2.13: CMakeToolchain generated: conan_toolchain.cmake
zlib/1.2.13: CMakeToolchain generated: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build/generators/CMakePresets.json
zlib/1.2.13: CMakeToolchain generated: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/src/CMakeUserPresets.json
zlib/1.2.13: Generating aggregated env files
zlib/1.2.13: Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
zlib/1.2.13: Calling build()
zlib/1.2.13: Apply patch (conan): separate static/shared builds, disable debug suffix, disable building examples
zlib/1.2.13: Running CMake.configure()
zlib/1.2.13: RUN: cmake -G "Ninja Multi-Config" -DCMAKE_TOOLCHAIN_FILE="generators/conan_toolchain.cmake" -DCMAKE_INSTALL_PREFIX="/home/chipc/.conan2/p/b/zlib2bd947a06b8af/p" -DCMAKE_POLICY_DEFAULT_CMP0091="NEW" "/home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/src"
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 3.5 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- Using Conan toolchain: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build/generators/conan_toolchain.cmake
-- Conan toolchain: Setting CMAKE_POSITION_INDEPENDENT_CODE=OFF (options.fPIC)
-- Conan toolchain: Setting BUILD_SHARED_LIBS = OFF
-- The C compiler identification is GNU 11.3.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/chipc/.conan2/p/b/tricoe43a847349e11/p/tricore-elf/bin/tricore-elf-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of off64_t
-- Check size of off64_t - failed
-- Looking for fseeko
-- Looking for fseeko - found
-- Looking for unistd.h
-- Looking for unistd.h - found
-- Renaming
--     /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/src/zconf.h
-- to 'zconf.h.included' because this file is included with zlib
-- but CMake generates it automatically in the build directory.
-- Configuring done (0.6s)
-- Generating done (0.0s)
-- Build files have been written to: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build

zlib/1.2.13: Running CMake.build()
zlib/1.2.13: RUN: cmake --build "/home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build" --config Release -- -j24
[16/16] Linking C static library Release/libz.a

zlib/1.2.13: Package '09b09ac0f270c2f8bb2a6716c0e57b1983c1682d' built
zlib/1.2.13: Build folder /home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build
zlib/1.2.13: Generating the package
zlib/1.2.13: Packaging in folder /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p
zlib/1.2.13: Calling package()
zlib/1.2.13: Running CMake.install()
zlib/1.2.13: RUN: cmake --install "/home/chipc/.conan2/p/b/zlib2bd947a06b8af/b/build" --config Release --prefix "/home/chipc/.conan2/p/b/zlib2bd947a06b8af/p"
-- Installing: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p/lib/libz.a
-- Installing: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p/include/zconf.h
-- Installing: /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p/include/zlib.h

zlib/1.2.13: package(): Packaged 1 file: LICENSE
zlib/1.2.13: package(): Packaged 1 '.a' file: libz.a
zlib/1.2.13: package(): Packaged 2 '.h' files: zconf.h, zlib.h
zlib/1.2.13: Created package revision ef1fde8ac197eadbd05b7e60a94d78f0
zlib/1.2.13: Package '09b09ac0f270c2f8bb2a6716c0e57b1983c1682d' created
zlib/1.2.13: Full package reference: zlib/1.2.13#4e74ebf1361fe6fb60326f473f276eb5:09b09ac0f270c2f8bb2a6716c0e57b1983c1682d#ef1fde8ac197eadbd05b7e60a94d78f0
zlib/1.2.13: Package folder /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p
WARN: deprecated: Usage of deprecated Conan 1.X features that will be removed in Conan 2.X:
WARN: deprecated:     'cpp_info.names' used in: zlib/1.2.13

======== Finalizing install (deploy, generators) ========
conanfile.py (foo/1.0): Calling generate()
conanfile.py (foo/1.0): Generators folder: /home/chipc/workspaces/conan/foo/build
conanfile.py (foo/1.0): Generating aggregated env files
conanfile.py (foo/1.0): Generated aggregated env files: ['conanbuild.sh', 'conanrun.sh']
Install finished successfully

@memsharded
Copy link
Member

That looks good.
Do you have some way to validate the resulting binary with some tool to check that it actually belongs to that architecture? The output for the compiler in CMake log seems ok with /home/chipc/.conan2/p/b/tricoe43a847349e11/p/tricore-elf/bin/tricore-elf-gcc, validating that the generated binary code belongs to the architecture would be fantastic.

If this works out of the box with the current CMake integration, it would be good enough to be merged in the default settings, and probably add some pointers or minimal guide in the docs.

@photex
Copy link
Author

photex commented May 23, 2024

Do you have some way to validate the resulting binary with some tool to check that it actually belongs to that architecture

I was just wondering about that. I'll see what I can dig up!

@photex
Copy link
Author

photex commented May 23, 2024

Ok, this was the best idea I had at the moment:

(.venv) [chipc@bucket foo]$ mkdir libz-objects
(.venv) [chipc@bucket foo]$ cd libz-objects/
(.venv) [chipc@bucket libz-objects]$ ar x /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p/lib/libz.a
(.venv) [chipc@bucket libz-objects]$ ls
adler32.c.obj   crc32.c.obj    gzclose.c.obj  gzread.c.obj   infback.c.obj  inflate.c.obj   trees.c.obj    zutil.c.obj
compress.c.obj  deflate.c.obj  gzlib.c.obj    gzwrite.c.obj  inffast.c.obj  inftrees.c.obj  uncompr.c.obj
(.venv) [chipc@bucket libz-objects]$ file gzlib.c.obj 
gzlib.c.obj: ELF 32-bit LSB relocatable, Siemens Tricore Embedded Processor, version 1 (SYSV), not stripped
(.venv) [chipc@bucket libz-objects]$

@memsharded
Copy link
Member

ELF 32-bit LSB relocatable, Siemens Tricore Embedded Processor, version 1 (SYSV), not stripped

Not bad, but not enough detail about the architecture in particular apparently. Of course running some executable in the actual HW would validate things, are you planning some kind of a full "hello world" full proof of concept involving the real HW?

@photex
Copy link
Author

photex commented May 23, 2024

That's a good idea. I bet I could adapt the blinky example from https://github.com/Infineon/AURIX_code_examples/tree/master/code_examples/Blinky_LED_1_KIT_TC397_TFT

Qemu might be an option, it has some early tricore support.

@photex
Copy link
Author

photex commented May 24, 2024

Ok, a hello world project will definitely take me some time that I don't have (for the moment, due to other work priorities). In the meantime though I did realize that I can use tricore-elf-readelf:

(.venv) [chipc@bucket libz-objects]$ tricore-elf-readelf -h deflate.c.obj 
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Infineon Tricore
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          31168 (bytes into file)
  Flags:                             0x100000, TC1.6.2
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         13
  Section header string table index: 12

I've also built my firmware using my branch and updated profiles to include the arch. And I still have to be sure and wiggle the -mtc162 flag in there via my CMake toolchain, for the zlib test it was enough to set the env var CFLAGS. The default for tricore-gcc is to use tc13. Which I can verify by removing the -mtc162 and rebuilding zlib:

(.venv) [chipc@bucket libz-objects]$ ar x /home/chipc/.conan2/p/b/zlib2bd947a06b8af/p/lib/libz.a
(.venv) [chipc@bucket libz-objects]$ tricore-elf-readelf -h deflate.c.obj 
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           Infineon Tricore
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          31452 (bytes into file)
  Flags:                             0x20000000, TC1.3
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         13
  Section header string table index: 12

Which makes me wonder whether other arch settings would work the same? For example: armv7hf, is there logic elsewhere in the Conan CMake tools that handle setting the relevant target flags for the toolchain? Or is it expected to be handled by profiles or developers every time?

@photex
Copy link
Author

photex commented May 24, 2024

Now I see: conan/tools/build/flags.py, will update that and rebuild without injecting the arch flags in the profile.

@photex photex closed this May 24, 2024
@photex
Copy link
Author

photex commented May 24, 2024

Oh no... I closed on accident. 😬

@memsharded memsharded reopened this May 24, 2024
@memsharded
Copy link
Member

Don't worry, PRs can be re-opened, done :)

Now I see: conan/tools/build/flags.py, will update that and rebuild without injecting the arch flags in the profile.

This is exactly what I was thinking of. Sometimes it is not enough to define the architecture, but some flags or something must be added to the build system integrations, typically in the toolchains (CMakeToolchain, AutotoolsToolchain, MesonToolchain,...), and some of these toolchains might use conan/tools/build/flags.py, for some common functionality so adding it there might help, but this is something to check in different build systems. At the moment given the nature of the compiler, I would focus on autotools and CMake, the others can wait for a second iteration.

@photex
Copy link
Author

photex commented May 24, 2024

I've pushed the little package I'm using to test with: https://github.com/photex/conan-tricore-test

I've included a test.log there which runs a build for 3 of the architectures and then prints their elf headers afterwards. Each builds and links with zlib using --whole-archive.

It's placed in this separate repo currently because I'm not yet certain how to integrate with the Conan testsuite properly (given that there isn't a toolchain for the arch on conancenter yet).

I have updated the flags, and the cmake toolchain generator.

In the generated conan_toolchain files I see the correct values for the system:

set(CMAKE_SYSTEM_NAME Generic-ELF)
set(CMAKE_SYSTEM_PROCESSOR tricore)

And the arch flags are generated correctly as well. For example:

string(APPEND CONAN_CXX_FLAGS " -mtc131")
string(APPEND CONAN_C_FLAGS " -mtc131")
string(APPEND CONAN_SHARED_LINKER_FLAGS " -mtc131")
string(APPEND CONAN_EXE_LINKER_FLAGS " -mtc131")

- Added arch target flags to conan/tools/build/flags.py
- Updated cmake/toolchain/blocks.py to reflect the expected system name and processor
Copy link
Member

@memsharded memsharded left a comment

Choose a reason for hiding this comment

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

This is looking good, I think it will be possible to merge it as it is low risk.

It would be good to add a couple of "unittest" tests:

If you need help with the tests, just let me know, I can contribute those tests too.

@photex
Copy link
Author

photex commented May 27, 2024

Thank you @memsharded I'll get those started asap.

@memsharded
Copy link
Member

There were some conflict with recent CMakeToolchain changes, I have tried to fix it. Also updated the first comment to include the Changelog line and reference the issue.

@photex
Copy link
Author

photex commented Jun 5, 2024

Just checking in real quick: work picked up but I'm still intending to add these unit tests once things cool down.

@memsharded
Copy link
Member

memsharded commented Jun 5, 2024

Just checking in real quick: work picked up but I'm still intending to add these unit tests once things cool down.

Don't worry, no hurries, just let me know when you can do it and we'll have a look and try to help to get this merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] [question] Support the Tricore-gcc toolchain and arch
3 participants