0

How do I compile different assembly files for different architectures in CMake? For example, I have x86_64.S for x86-64, x86_32.S for x86-32, ARM7.S for ARMv8 and AArch64.S for ARMv8 AArch64. How to compile only one assembly file for target architecture?

1 Answers1

1

Edit: As mentioned by @KamilCuk, there is no need to use the add_custom_command() cmake can detect automatically how to manage the source file.

add_executable(my_exec main.c out.o)

if( CMAKE_SYSTEM_PROCESSOR MATCHES "arm" )
    target_sources(my_exec ARMV7.S)
elseif( CMAKE_SYSTEM_PROCESSOR MATCHES "x86" )
    target_sources(my_exec x86_64.S)
endif()



According to this answer How to detect target architecture using CMake?, you could add a custom command depending of the architecture. The following could help:

if( CMAKE_SYSTEM_PROCESSOR MATCHES "arm" )
    add_custom_command(
        OUTPUT out.o 
        COMMAND as -o out.o ARMV7.S 
        DEPENDS ARMV7.S
    )
elseif( CMAKE_SYSTEM_PROCESSOR MATCHES "x86" )
    add_custom_command(
        OUTPUT out.o 
        COMMAND nasm -o out.o x86_64.S
        DEPENDS x86_64.S
    )
endif()

add_executable(my_exec main.c out.o)

Adapt conditions to fit your needs.

  • 1
    You do not really need add_custom_command, just `target_sources(my_exec ARMV7.S)` should be enough. – KamilCuk May 10 '22 at 20:49