8

I have two small files that I want to compile in CMake for debugging it with CLion and GDB

main.c

int my_strlen(char *);

int main()
{
    printf("Test: %d\n", my_strlen("Hello"));
}

and I have my ASM file that have a my_strlen file

    [BITS 64]

    global my_strlen
    section .text

my_strlen:
    push rbp
    mov rbp, rsp
    xor rcx, rcx

loop:
    cmp BYTE [rdi + rcx], 0
    jz end
    inc rcx
    jmp loop

end:
    mov rax, rcx
    mov rsp, rbp
    leave
    ret

I'm trying to compile with a CMakeList.txt I add set_source_files_properties but it still doesn't work

cmake_minimum_required(VERSION 3.9)
project(ASM)

set(CMAKE_CXX_STANDARD 11)
set_source_files_properties(src/strlen.asm PROPERTIES COMPILE_FLAGS "-x assembler-with-c")

add_executable(main
        src/strlen.asm
        tests/src/main.c)

Someone knows a good command to add ASM file in a C project and compile with a CMakeList.txt ?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Benjamin Sx
  • 653
  • 1
  • 7
  • 18
  • `assembler-with-c` - where in the docs did you see that flag? And you have two files, one ASM and one C. You don't have a "assembly with C" source. – Mat Mar 06 '18 at 13:33
  • I see it on a website and I try with -g too but it's true I didn't find too – Benjamin Sx Mar 06 '18 at 13:33
  • 3
    “it doesn't work” is not an error description. What exactly happens when you try that? Also note that this probably only works if your file is in gas syntax which it isn't. You probably need to write your own rule to invoke nasm or whatever assembler you use. – fuz Mar 06 '18 at 14:19

1 Answers1

12

You'll probably have to enable NASM support in CMAKE with enable_language(ASM_NASM)and then set appropriate assembly options:

cmake_minimum_required(VERSION 3.9)
project(ASM)
enable_language(ASM_NASM)
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
    <FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")

set_source_files_properties(src/strlen.asm PROPERTIES COMPILE_FLAGS "-g -Fdwarf")

set(CMAKE_CXX_STANDARD 11)

add_executable(main
        src/strlen.asm
        tests/src/main.c)

Since your code seems to be a 64-bit target I essentially pass -f elf64 as a command line option to NASM. I place the format type in its own environment variable CMAKE_ASM_NASM_OBJECT_FORMAT. To enable debug information in NASM you can use the -g -Fdwarf.


If doing Debug and Release builds you could check for the build type and set the flags accordingly with something like:

cmake_minimum_required(VERSION 3.9)
project(ASM)
enable_language(ASM_NASM)

set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
set(CMAKE_ASM_NASM_COMPILE_OBJECT "<CMAKE_ASM_NASM_COMPILER> <INCLUDES> \
    <FLAGS> -f ${CMAKE_ASM_NASM_OBJECT_FORMAT} -o <OBJECT> <SOURCE>")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_ASM_NASM_FLAGS "${ASM_NASM_FLAGS} -g -Fdwarf")
else()
    set(CMAKE_ASM_NASM_FLAGS "${ASM_NASM_FLAGS}")
endif()

set(CMAKE_CXX_STANDARD 11)

add_executable(main
        src/strlen.asm
        tests/src/main.c)
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    It's work great. but with CLion I can't add breakpoint in ASM but its a another question, thannks for help. – Benjamin Sx Mar 06 '18 at 17:04
  • 1
    @BenjaminSx What platform are you on. I assumed Linux? Maybe you are on Windows or MacOS? On Linux I tried your files and my version of the CMakeList.txt (with CLion 2017.3) and was able to bring up the assembly file set a break point on my_strlen . When I ran the debug version it did stop on the breakpoint. – Michael Petch Mar 06 '18 at 22:32
  • 1
    I'm using linux thanks for the information so I need to investigate were's the problem. – Benjamin Sx Mar 10 '18 at 16:07
  • I miss the documentation for these variables. I could find [The source code](https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/CMakeASM_NASMInformation.cmake), but is it explained somewhere in the CMake documentation. Could you please give a reference? – St.Antario Aug 09 '19 at 17:19
  • 1
    @St.Antario See [here](https://gitlab.kitware.com/cmake/cmake/blob/master/Auxiliary/vim/syntax/cmake.vim#L487-604), from [this answer](https://stackoverflow.com/questions/56420035) – Alexis Wilke Apr 01 '20 at 04:53