27

I'm using eclipse for building a avr-gcc project that mixes assembly code and C source files. I want to get rid of the automatic makefile generation of eclipse because I need to automate some process into the makefiles and for other reasons.

I used cmake some times ago and I was happy with it so I want to try to compile my source files using it. Everything run as expected with C sources. The problem is that at the end I need to compile some assembly files (actually 2) and add them to the target.

I googled around but I didn't found a way for doing this. someone have an idea on how to do this?

The problem is that in eclipse I have -x assembler-with-cpp

added to gcc argument list. I need to find a way for selectively add this param to the standard gcc argument list only for the asm files. I didn't find around any way for doing this.

thank you in advance

SOLUTION: set in CMakeLists.txt every file to compile in the same list

enable_language(C ASM)

set ( SOURCES 
    foo.c
    bar.c
    foobar.s
)

add_executable(program  ${SOURCES} ) 

in the Toolchain file you should place:

SET(ASM_OPTIONS "-x assembler-with-cpp")
SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}" )

the second line is just if you need to pass extra options while compiling asm files. I wanted to pass all the CFLAGS plus some ASM_OPTIONS

Uli Köhler
  • 11,813
  • 14
  • 55
  • 105
andyinno
  • 871
  • 2
  • 7
  • 22
  • 1
    Can you describe more precisely what the problem is with compiling the assembler sources? – Mats Petersson Feb 28 '13 at 10:57
  • I edited my post. please see it for new details. I hope they are enougth for understand what's the problem. thank you again – andyinno Feb 28 '13 at 13:30
  • I don't understand. You don't want to use Eclipse-generated makefiles, and complain about the Makefile Eclipse writes? – vonbrand Feb 28 '13 at 20:14
  • I don't want to use the auto generated makefiles of eclipse because I need to place the build system in a jenkins enviroment on the server. The auto generated makefile have hardcoded path and isn't suitable for being used in jenkins. The alternatives I found was write my own makefile or use something like cmake. cmake works perfectly for everything. I just have problems compiling asm files. I'm not a cmake guru, I used it in the past for simple projects. – andyinno Mar 01 '13 at 08:14

2 Answers2

29

CMake supports assembler out of the box. Just be sure to enable the "ASM" language in your project. If an assembler source file needs preprocessing, also set the source file's compilation options:

project(assembler C ASM)
set_property(SOURCE foo.s APPEND PROPERTY COMPILE_OPTIONS "-x" "assembler-with-cpp")
add_executable(hello foo.s bar.c)
sakra
  • 53,539
  • 13
  • 152
  • 136
  • Thank you for your reply. THis don't work at the moment... I have the problem compiling this file: https://github.com/metormote/hardware/blob/master/basestation_test/src/asf/xmega/drivers/cpu/ccp.s If I use what you wrote the file is called directly by avr-as. I think I need to call the file using avr-gcc for let it preprocess the file. – andyinno Feb 28 '13 at 17:23
  • Thank you sakra for your comment. I tryed this: set ( ASM_SOURCES xmega/drivers/nvm/nvm_asm.s xmega/drivers/cpu/ccp.s ) set_source_files_properties(${ASM_SOURCES} PROPERTIES COMPILE_FLAGS "${CFLAGS} -x assembler-with-cpp") add_executable(xMegaAnalog ${SOURCES} ${ASM_SOURCES}) the problem is that it looks like the files won't be compiled but just skipped. I don't see them using make VERBOSE=1 – andyinno Mar 01 '13 at 08:41
  • 1
    Be sure to enable the ASM language, either by using `project(assembler C ASM)` or `enable_language(C ASM)`. – sakra Mar 01 '13 at 09:02
  • this is on top of CMakeLists.txt: Project(XmegaAnalog) project(assembler C ASM) I tryed to join the 2 in something like Project(XmegaAnalog assembler C ASM) but with no results – andyinno Mar 01 '13 at 09:07
  • try using `enable_language(C ASM)`. – sakra Mar 01 '13 at 12:01
  • I get it working... thank you for your help. with enable_language(C ASM) it try to compile the file but with errors. The solution was placing a new variable with SET in the arch file SET(ASM_OPTIONS "-x assembler-with-cpp") and enabling it for asm sources with SET(CMAKE_ASM_FLAGS "${CFLAGS} ${ASM_OPTIONS}" ). *.s files are in this case receiving the right arguments for compiling nicely. – andyinno Mar 01 '13 at 12:52
5

Based on your solution, one of the simplest solutions is this one-liner:

SET(CMAKE_ASM_FLAGS "${CFLAGS} -x assembler-with-cpp")
Uli Köhler
  • 11,813
  • 14
  • 55
  • 105