Reputation: 186
I have an assembler file I want to compile in one run. However, the following code fails:
enable_language(ASM_NASM)
set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -f bin")
add_executable(test test.s)
CMake first runs: nasm -f bin -o test.s.o test.s
And then: nasm -f bin -o test test.s.o
The last step fails as test.s.o is already a binary file.
My question is: How do I disable the first compilation step?
Upvotes: 7
Views: 2355
Reputation: 587
I hope you solved your problem, however I feel like I need to post here my solution just because that's the only mention of this problem and it doesn't seem to be solved by the only given answer.
Indeed, nasm
can compile sources directly to .bin
format (and that's really helpful for boot sectors). The problem is that Cmake
doesn't seem to support single stage compilation and linking and it tries to link resulting .bin
file, which is imposseible, so the execution fails no matter which linker we try to use.
The only solution that came to my mind was replacing add_executable(NAME SOURCE)
with add_library(NAME OBJECT SOURCE)
. Object libraris are not linked, so the file produced will be exactly what we are looking for.
And here is the complete solution:
enable_language(ASM_NASM)
set(CMAKE_ASM_NASM_OBJECT_FORMAT bin)
add_compile_options("$<$<COMPILE_LANGUAGE:ASM_NASM>:-f bin>")
add_library(lib OBJECT core/boot/loader.asm)
The tricky part: the generated file will be hidden within cmake
output dir. It sould be referenced with $<TARGET_OBJECTS:lib>
.
Enjoy!
Upvotes: 0
Reputation: 71
There seems to be a bug in nasm module for cmake. Cmake calls nasm for linking which is obviously wrong (that is why you see two calls to nasm). Hotfix is to set
set(CMAKE_ASM_NASM_LINK_EXECUTABLE "ld <FLAGS> <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
Upvotes: 7