# Having cmake to generate list files as per source file

**URL:** https://discourse.cmake.org/t/having-cmake-to-generate-list-files-as-per-source-file/6191
**Category:** Code
**Tags:** gen:ninja
**Created:** [July 31, 2022, 4:49pm UTC](https://discourse.cmake.org/t/having-cmake-to-generate-list-files-as-per-source-file/6191 "2022-07-31T16:49:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fruitCoder123](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/e274bd/32.png) [@fruitCoder123](https://discourse.cmake.org/u/fruitCoder123)
#### Post date: [July 31, 2022, 4:49pm UTC](https://discourse.cmake.org/t/having-cmake-to-generate-list-files-as-per-source-file/6191/1 "2022-07-31T16:49:20Z")

</div>

GCC offers an assembler option to generate list files:

```auto
-Wa,-adhln=<path/file.lst>

```

I know my way around in cmake but I am pulling my hair how to get this in. My struggle is because it is per source file that I like to have the option and the output file is based on the source file name.

Note1: I am crosscompiling for renesas RL78 but that should not matter  
Note2: If I run this command manually it works; the list file is generated

So to be clear I like the compile steps to be like this:

rl78-elf-gcc.exe -ID:/sources/RL78/dev\_brd\_F13\_gcc\_led\_blink/…/includes/R5F10BMG -mg14 -mmul=g14 -Wstack-usage=40 -ffunction-sections -fdata-sections -nostdlib -DDEBUG -O0 -g **-Wa,-adlhn=CMakeFiles\rl78\_led\_blink.dir\src\main.lst** -MD -MT CMakeFiles/rl78\_led\_blink.dir/src/main.c.obj -MF CMakeFiles\rl78\_led\_blink.dir\src\main.c.obj.d -o CMakeFiles/rl78\_led\_blink.dir/src/main.c.obj -c D:/sources/RL78/dev\_brd\_F13\_gcc\_led\_blink/src/main.c

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [August 2, 2022, 11:14am UTC](https://discourse.cmake.org/t/having-cmake-to-generate-list-files-as-per-source-file/6191/2 "2022-08-02T11:14:25Z")

</div>

AFAIK, there’s no way to add such a flag except one-by-one. You can set the source file property `COMPILE_OPTIONS` to contain this, but there’s no way to say “where the object file will be written” to get its directory (yet?).

---

<div class="post-metadata">

### Author: ![fruitCoder123](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/e274bd/32.png) [@fruitCoder123](https://discourse.cmake.org/u/fruitCoder123)
#### Post date: [September 4, 2022, 7:36pm UTC](https://discourse.cmake.org/t/having-cmake-to-generate-list-files-as-per-source-file/6191/3 "2022-09-04T19:36:52Z")

</div>

Thanks @ben.boeckel for the hint. As I reference I like to paste my final solution here for anybody who can use it. The list files end up at the location where the source file resides. If one uses git a simple "git clean -dxf’ will remove them (and all other files that are not registered in the git project!)

> cmake\_minimum\_required(VERSION 3.0)
> 
> project(test\_list\_files)
> 
> include\_directories(hdr)
> 
> set (headers  
> hdr/add.h  
> hdr/subtract.h  
> hdr/multiply.h  
> hdr/divide.h  
> )
> 
> set (sources  
> src/main.c  
> src/add.c  
> src/subtract.c  
> src/multiply.c  
> src/divide.c  
> )

**foreach(source\_file IN LISTS sources)**  
**Message(STATUS ${source\_file})**  
**set\_source\_files\_properties(${source\_file} PROPERTIES COMPILE\_OPTIONS “-Wa,-adhln=${CMAKE\_CURRENT\_SOURCE\_DIR}/${source\_file}.lst”)**  
**endforeach()**

> add\_executable(${PROJECT\_NAME} ${sources} ${headers})

---

<div class="post-metadata">

### Author: ![fruitCoder123](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/f/e274bd/32.png) [@fruitCoder123](https://discourse.cmake.org/u/fruitCoder123)
#### Post date: [September 4, 2022, 7:40pm UTC](https://discourse.cmake.org/t/having-cmake-to-generate-list-files-as-per-source-file/6191/4 "2022-09-04T19:40:47Z")

</div>

I post my final solution here for anyone to use it. I made a simple example with gcc.  
List files end up where the source files reside.

```cmake
cmake_minimum_required(VERSION 3.0)

project(test_list_files)

include_directories(hdr)

set (headers
    hdr/add.h
    hdr/subtract.h
    hdr/multiply.h
    hdr/divide.h
)

set (sources 
    src/main.c
    src/add.c
    src/subtract.c
    src/multiply.c
    src/divide.c
)

foreach(source_file IN LISTS sources)
    set_source_files_properties(${source_file} PROPERTIES COMPILE_OPTIONS "-Wa,-adhln=${CMAKE_CURRENT_SOURCE_DIR}/${source_file}.lst")
endforeach()

add_executable(${PROJECT_NAME} ${sources} ${headers})

```
