2

I've been using a .bat file to build my applications for years. Recently, switched to CMake for it's elegancy and I ran into a situation where I had to guess the flags that I should put into CMake's add_link_options and add_compile_options

Example flags that I've used in my BAT file,

-s WASM=1 --bind -s MODULARIZE=1

And, In CMake this flags have become (After trial and error),

add_compile_options("SHELL: -s WASM=1")
add_link_options("SHELL: --bind")
add_link_options("SHELL: -s MODULARIZE=1")

Honestly, I can't find any information regards flags that add_link_options and add_compile_options supports.

I know what is a linker is but lost when it comes to add_link_options or linker flags.

I'm used to compile everything in single line and now in CMake everything appear to be involve separate steps.

jeffbRTC
  • 1,941
  • 10
  • 29
  • Nothing. You should not be using those, but the `target_*` variants. But more generally, read a good guide such as https://cliutils.gitlab.io/modern-cmake/ – spectras Dec 13 '20 at 02:11
  • I have target_* variants too and since I have multiple targets I have common flags that has to be shared among them. But either way, be it target or non-target, I still have no idea what to do regarding flags. – jeffbRTC Dec 13 '20 at 02:36
  • When you run `make clean && make VERBOSE=1`, does it show compiling and linking as two separate steps? Does it show `--bind` in the linker command and `WASM=1` in the compiler command? Commands run with `-c` are compilation, not linking. It seems to me that maybe all 3 options you're passing belong to the compiler phase, but I could be wrong. It would help if you tell us if your project is building OK now, or if there is a problem, what it is. – John Zwinck Dec 13 '20 at 11:04

2 Answers2

1

I am not sure what your problem is, but here is a full working sample from a Wasm project that sets project-wide strict mode and disabling of exception support:

if (EMSCRIPTEN)
    add_compile_options(-fno-exceptions "SHELL:-s STRICT=1")
    add_link_options("SHELL:-s STRICT=1")
endif()

Note in particular that, as it has a [compile+link] marker in the emscripten settings, -s STRICT=1 has to be used both for compiling and for linking, thus it appears in each.

The if(EMSCRIPTEN) around is there because this project can also be built for Windows and Linux.

spectras
  • 13,105
  • 2
  • 31
  • 53
  • while -s STRICT=1 behaves so other flags can't be used that way. I just need to know which flags I should pass at compile and linker stages. – jeffbRTC Dec 13 '20 at 15:57
  • Simply look them up in the documentation. Clang documentation is here: https://clang.llvm.org/docs/ClangCommandLineReference.html and emscripten flags documentation is here: https://github.com/emscripten-core/emscripten/blob/master/src/settings.js – spectras Dec 13 '20 at 16:02
  • Have you checked? What make you thinks that I didn't checked those? I did and they contains no info. – jeffbRTC Dec 13 '20 at 16:21
  • The Github one does contains them. I initially used their website docs. Now everything clear. If you can write answer, I will accept it. – jeffbRTC Dec 13 '20 at 16:27
  • Overall emscripten documentation is good, but a few technical details are harder to find than they should be. The link for that page is on [EMCC reference](https://emscripten.org/docs/tools_reference/emcc.html), about 2 pages down the top, in the documentation for `-s OPTION[=VALUE]`. – spectras Dec 13 '20 at 17:01
  • When one shall use quoted string and when one may provide options verbatim? Where did "SHELL: " syntax come from? – ivan_onys Aug 24 '21 at 17:54
  • 1
    @ivan_onys> straight from [the documentation of add_compile_options](https://cmake.org/cmake/help/latest/command/add_compile_options.html) – spectras Aug 24 '21 at 18:22
0

The options you can pass to the compiler or linker depends on which compiler or linker you use. For example if you fork GCC and add a -Wstackoverflow-copy-pasta option, you can pass that option to add_compile_options(), but other people using standard GCC cannot.

So the answer to your question seems to be, read your compiler and linker documentation.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I'm using Emscripten and it uses Clang I believe. – jeffbRTC Dec 13 '20 at 02:49
  • I have read documentation and it lacks this info that's why I posted this question in first place. – jeffbRTC Dec 13 '20 at 02:50
  • Can you edit your question to include the specific flags from your old .bat files that you're not sure how to integrate in CMake? Maybe also try `make VERBOSE=1` with your CMake project to see what options it is passing where, and compare those with your old .bat files. – John Zwinck Dec 13 '20 at 03:05
  • I already have integrated everything but just feel uncomfortable with guessing game. I will edit and include flags. – jeffbRTC Dec 13 '20 at 03:13
  • I just added them to post. – jeffbRTC Dec 13 '20 at 03:16
  • @jeffbRTC: As far as I understand from the documentation, Emscripten uses `emcc` as a compiler and as a linker. According to [that answer](https://stackoverflow.com/a/53133873/3440745), `-s` is a **linker** option. – Tsyvarev Dec 13 '20 at 09:35
  • @Tsyvarev emcc use Clang and -s is not just a linker option because -s WASM=1 isn't – jeffbRTC Dec 13 '20 at 15:55