iarsystems / cmake-tutorial

Build and test embedded software using the IAR C/C++ Compiler alongside CMake
https://iar.com
MIT License
76 stars 14 forks source link

[Question] how to enable multifile compilation #36

Closed tarek-bochkati closed 5 months ago

tarek-bochkati commented 6 months ago

First thanks for this great guidance on how to use CMAKE with IAR Compiler.

I'm using CMake to automate benchmark tasks, but I'm facing an issue enabling --mfc option to compile all source code in one shot to benefit from multi file compilation technic.

Could you please help me achieve that using CMake & IAR Compiler

felipe-iar commented 6 months ago

Hi @tarek-bochkati, thank you very much for your feedback and also for your great question!

Though, if we consider the --mfc parameter requirements, it would lead us to:

iccarm file1.c file2.c file3.c --mfc ...

whereas target_sources() doesn't seem to provide a way for specifying file sets for such purpose.

At first sight, I'd consider one of the following approaches:

1) Include *.c sources into a single compilation unit

/* File: main .c */

#include "foo1.c"
#include "foo2.c"
/* ... */

void main() { 
/* ... */

2) Trick CMake passing the file set as target_compile_options()

cmake_minimum_required(VERSION 3.20)

project(Tutorial)

add_executable(tutorial)

target_sources(tutorial
  PRIVATE
    tutorial.c
)

set(MFC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/foo1.c ${CMAKE_CURRENT_SOURCE_DIR}/foo2.c)

target_compile_options(tutorial PRIVATE ${MFC_FILES} --mfc --discard_unused_publics --cpu=cortex-m4 -e)

where the output from cmake --build . --clean-first --verbose was:

[1/2] /opt/iarsystems/bxarm/arm/bin/iccarm --silent /home/felipeto/cmake-tutorial/tutorial/tutorial.c   /home/felipeto/cmake-tutorial/tutorial/foo1.c /home/felipeto/cmake-tutorial/tutorial/foo2.c --mfc --discard_unused_publics --cpu=cortex-m4 -e --dependencies=ns CMakeFiles/tutorial.dir/tutorial.o.d -o CMakeFiles/tutorial.dir/tutorial.o

[!TIP] According to the User Guide, the option --discard_unused_publics might be helpful in scenarios where --mfc is used within a project-wide scope, where the compiler can have full visibility of the source code from a single compilation unit.

Hope it helps. Let me know!

tarek-bochkati commented 6 months ago

Thanks @felipe-iar for you proposals. Proposal 2 seems more reasonable, since it does not require modifying the source code to fit with compilation needs, that are subject to change depending on optimization profile.

I will try, and let you know how it goes.

Thanks !

felipe-iar commented 6 months ago

Thanks, @tarek-bochkati. Looking forward for further feedback.

felipe-iar commented 5 months ago

Hi @tarek-bochkati,

I added a new entry on the wiki related to these suggestions: https://github.com/IARSystems/cmake-tutorial/wiki/Multi%E2%80%90file-compilation

Whenever possible, please let us know how proposal 2 went for your project(s). Thanks!

felipe-iar commented 5 months ago

Closing this due inactivity. Please feel free to reach out to us for further discussions!