# How to get listing file when cross compile

**URL:** <https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [December 4, 2020, 1:20pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297 "2020-12-04T13:20:34Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Massimiliano\_Cialdi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/massimiliano_cialdi/32/924_2.png) [@Massimiliano\_Cialdi](https://discourse.cmake.org/u/Massimiliano_Cialdi)\
**Post date:** [December 4, 2020, 1:20pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/1 "2020-12-04T13:20:34Z")

</div>

I’m using cmake 3.16.3 on xubuntu 20.04 to cross-compile a bare metal project that will run on ARM cortex-m7 microcontroller.  
The project is structured in subdirectories, and for this I use the `add_subdirectory()` command.

I need the listing files created at compilation time (with the option `-Wa,-anhlmsd=<file_name>.lst`).

How can I do this with `cmake`?

bast regards  
Max

---

<div class="post-metadata">

**Author:** ![sdettmer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sdettmer](https://discourse.cmake.org/u/sdettmer)\
**Post date:** [December 4, 2020, 4:32pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/2 "2020-12-04T16:32:24Z")

</div>

Hi,

if I understand correctly you like to have the current filename inside CFLAGS. I don’t think there is direct support for it. I think you could create file specific CFLAGS using a loop and

```auto
get_property(allsrcfiles TARGET theliborwhatever PROPERTY SOURCES)
foreach(srcfile IN LIST allsrcfiles)
  set_source_files_properties(${srcfile} PROPERTIES COMPILE_FLAGS "-Wa,-anhlmsd=${srcfile}.lst")
endforeach()

```

possibly remove some path from srcfile in the loop.

We had a similar issue and for several reasons I won’t explain here for us a GNU Make specific solution was the best for us:

```auto
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-builtin-macro-redefined -D' __FILE__ =\"$(subst ${SRCROOT},./build,$(subst ${SRCROOT},.,$(realpath $<)))\"'")

```

The trick is that make sets “$\<”, realpath makes it absolute, we ensure to have an absolute SRCROOT variable set and make applies the $(subst) function at the right time.

**Some consider this dealing with black magic, it breaks any other Generator except make and your mileage may vary.**

---

<div class="post-metadata">

**Author:** ![sdettmer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sdettmer](https://discourse.cmake.org/u/sdettmer)\
**Post date:** [December 4, 2020, 4:37pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/3 "2020-12-04T16:37:07Z")

</div>

So maybe you need something like:

```auto
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} '-Wa,-anhlmsd=\"$(notdir $<)\"'")

```

(untested)

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Cialdi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/massimiliano_cialdi/32/924_2.png) [@Massimiliano\_Cialdi](https://discourse.cmake.org/u/Massimiliano_Cialdi)\
**Post date:** [December 4, 2020, 5:35pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/4 "2020-12-04T17:35:44Z")

</div>

> [@sdettmer](#):
>
> if I understand correctly you like to have the current filename inside CFLAGS.

Yes…

> [@sdettmer](#):
>
> possibly remove some path from srcfile in the loop.

In fact I would like the `<file>.c.lst` files to be written in the same location where `<file>.c.o` and `<file>.c.d` are located.  
I looked among the variables, but I didn’t find what I was looking for. Before going to browse through the cmake modules, could you suggest how I can do it?

> [@sdettmer](#):
>
> We had a similar issue and for several reasons I won’t explain here for us a GNU Make specific solution was the best for us:

I would like that my solution could not depend on `GNU make` (for example we would like to test also `ninja`). Everything depends on other features that I hope you can insert

best regards  
Max

---

<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:** [December 4, 2020, 8:43pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/5 "2020-12-04T20:43:30Z")

</div>

> [@Massimiliano\_Cialdi](#):
>
> I looked among the variables, but I didn’t find what I was looking for. Before going to browse through the cmake modules, could you suggest how I can do it?

I think we’d need a new generator expression. Something like `$<TARGET_OBJECT_DIR:tgt>` perhaps (in order to support multi-config generators)? The `foreach` loop is likely to continue to be necessary without some other feature that would need more work. Could you please file a feature request on [the issue tracker](https://gitlab.kitware.com/cmake/cmake/-/issues)?

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Cialdi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/massimiliano_cialdi/32/924_2.png) [@Massimiliano\_Cialdi](https://discourse.cmake.org/u/Massimiliano_Cialdi)\
**Post date:** [December 5, 2020, 9:29pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/6 "2020-12-05T21:29:52Z")

</div>

> [@ben.boeckel](#):
>
> I think we’d need a new generator expression. Something like `$<TARGET_OBJECT_DIR:tgt>` perhaps (in order to support multi-config generators)? The `foreach` loop is likely to continue to be necessary without some other feature that would need more work. Could you please file a feature request on [the issue tracker](https://gitlab.kitware.com/cmake/cmake/-/issues)?

In the meantime I could browse cmake modules files, to see how the .o path is composed, so that I could copy it.  
Do you think this is a viable way?

best regards  
Max

---

<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:** [December 5, 2020, 9:42pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/7 "2020-12-05T21:42:45Z")

</div>

This would probably work in most cases. Multi-config generators (IDEs) would be the main thing to watch out for.

---

<div class="post-metadata">

**Author:** ![sdettmer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sdettmer](https://discourse.cmake.org/u/sdettmer)\
**Post date:** [December 7, 2020, 9:45pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/8 "2020-12-07T21:45:40Z")

</div>

Couldn’t we get property LIBRARY\_OUTPUT\_DIRECTORY and append basename and “.lst”?

---

<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:** [December 7, 2020, 10:38pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/9 "2020-12-07T22:38:48Z")

</div>

No, that is where the linked library ends up. The `.o` (or `.obj`) files end up in a different place that is not accessible via a property (and I don’t think it can be since some generators don’t specify where they’ll end up, so CMake can’t give a reliable answer).

---

<div class="post-metadata">

**Author:** ![Russell\_Schultz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/russell_schultz/32/282_2.png) [@Russell\_Schultz](https://discourse.cmake.org/u/Russell_Schultz)\
**Post date:** [December 11, 2020, 3:14pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/10 "2020-12-11T15:14:15Z")

</div>

As mentioned in the thread that linked to this, I’ve found that generator expressions don’t seem to be expanded when generating the link command.

In my toolchain file I have:  
`set(CMAKE_EXE_LINKER_FLAGS "--std=c++11 -Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${exe_name}.map")`

And I end up with

`-Wl,-Map=$<TARGET_FILE_DIR:tgt>/mapfile.map`

in the eventual call.

---

<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:** [December 11, 2020, 3:42pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/11 "2020-12-11T15:42:06Z")

</div>

No, that wouldn’t work I imagine. You’ll want `target_link_options` which does handle generator expressions.

---

<div class="post-metadata">

**Author:** ![Russell\_Schultz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/russell_schultz/32/282_2.png) [@Russell\_Schultz](https://discourse.cmake.org/u/Russell_Schultz)\
**Post date:** [December 11, 2020, 3:53pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/12 "2020-12-11T15:53:13Z")

</div>

Well, that’s the problem. I have to have special logic in my project description to add “the right” linker flags for the current toolchain for every target.

It means I end up wrapping add\_executable() with something, but it’s doable.

It seems like it would make life easier if the expansion of these variables took advantage of generator expressions. I don’t know if generator expressions can do it, but it would also be nice to be able to query target properties and expand to different things based on them.

This would allow the toolchain file to be a one stop shop for fleshing out compiler support.

`set(CMAKE_EXE_LINKER_FLAGS $<IF:$<STREQUAL:$<TARGET_PROPERTY:MAKE_LISTFILE>,"true">,"-Wl,-Map=$<TARGET_FILE_DIR:tgt>/mapfile.map","">)`

(I think that syntax is right, but you get the idea.) if the target that’s getting currently built has the property, add this string to the linker flags, expanding the target directory.

---

<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:** [December 11, 2020, 5:39pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/13 "2020-12-11T17:39:10Z")

</div>

We’d need a new placeholder to say “the current target being compiled” to replace that literal `tgt`, but yeah, something like that looks mostly OK (quoting is off, but can just be refactored out into the whole thing). A feature request for genex support (and the “current target name” replacement) would be the way to go.

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Cialdi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/massimiliano_cialdi/32/924_2.png) [@Massimiliano\_Cialdi](https://discourse.cmake.org/u/Massimiliano_Cialdi)\
**Post date:** [December 15, 2020, 10:51am UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/14 "2020-12-15T10:51:11Z")

</div>

> [@ben.boeckel](#):
>
> No, that is where the linked library ends up. The `.o` (or `.obj`) files end up in a different place that is not accessible via a property (and I don’t think it can be since some generators don’t specify where they’ll end up, so CMake can’t give a reliable answer).

If I understand what I’m reading, this would mean that it’s impossible to _drive_ CMake to configure the build-time writing of `.lst` files at the location I want and with the name I want.  
Am I understanding this correctly?

best regards  
Max

---

<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:** [December 15, 2020, 3:18pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/15 "2020-12-15T15:18:56Z")

</div>

As of today, yes. I proposed a genex for this further up the thread:

> [@ben.boeckel](#):
>
> I think we’d need a new generator expression. Something like `$<TARGET_OBJECT_DIR:tgt>` perhaps (in order to support multi-config generators)?

That should be suitable to get “where the `.o` files for `tgt` will appear”.

---

<div class="post-metadata">

**Author:** ![Russell\_Schultz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/russell_schultz/32/282_2.png) [@Russell\_Schultz](https://discourse.cmake.org/u/Russell_Schultz)\
**Post date:** [January 1, 2021, 8:52pm UTC](https://discourse.cmake.org/t/how-to-get-listing-file-when-cross-compile/2297/16 "2021-01-01T20:52:36Z")

</div>

I’ve been lazy, but I finally requested it here: [Feature Request: Implement genex that gives access to the target directory and filename (#21650) · Issues · CMake / CMake · GitLab (kitware.com)](https://gitlab.kitware.com/cmake/cmake/-/issues/21650)

I worked around it for my mapfiles until I got burned by the same thing trying to generate list files per .o compiled.
