<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59776>59776</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Building libunwind for wasm32 NDEBUG is not defined causing inclusion of logAPIs symbol
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          HaydnTrigg
      </td>
    </tr>
</table>

<pre>
    I am building a modified libunwind against a wasm32 target with the `RelWithDebugInfo` build type.

When linking I get the following error:
```
1>wasm-ld : error : wasi-sysroot-17.0/wasi-sysroot/lib\libunwind.a(Unwind-wasm.c.o): undefined symbol: logAPIs
```

Inside of [Unwind-wasm.c](https://github.com/ChimpsAtSea/llvm-project/blob/wasm-unwinding/libunwind/src/Unwind-wasm.c) there is usage of the `_LIBUNWIND_TRACE_API` which is only supposed to use the function `logAPIs` in a debug build of libunwind.

I have tracked down the issue looking at the command invocation that CMake is not supplying the `NDEBUG` macro by default. This causes the `_LIBUNWIND_TRACE_API` macro to be defined causing the inclusion of the `logAPIs` symbol.

There is a bit of nonsense inside of the libunwine CMake file that doesn't make a lot of sense to me and I think it might be wrong. The [following code block](https://github.com/llvm/llvm-project/blob/main/libunwind/CMakeLists.txt#L257)
```
# Assert
string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
if (LIBUNWIND_ENABLE_ASSERTIONS)
  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
  if (NOT MSVC)
 add_compile_flags(-D_DEBUG)
  endif()

  # On Release builds cmake automatically defines NDEBUG, so we
  # explicitly undefine it:
  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
 add_compile_flags(-UNDEBUG)
  endif()
else()
  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
 add_compile_flags(-DNDEBUG)
  endif()
endif()
```

The statement that CMake automatically defined NDEBUG appears to not always be true. And assuming that might actually be the case. The very last branch statement is supposed to add the NDEBUG flag, but the statement will only add that if the build is a debug build, which seems wrong. Should a debug build actually be defining NDEBUG?

I changed the statement from `if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")` to `if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")` and that has resolved the issue of NDEBUG missing from the define statements on the command line.

I'm not aware of any knock on effects that might have and I'm not aware of why libunwind would not have NDEBUG defined for it already by default if that is intended default behavior. But I figure I'd open this issue to raise my observations.

Alternatively, would it make more sense to change the `#ifdef` statement in config.h from using `NDEBUG` to checking for the presence of `DEBUG` instead?

I have an easy workaround that is simply manually defining NDEBUG to appear on the command line through `CMAKE_***_FLAGS`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV11v6jwS_jXmZtQoNV_lggso8C7anp5uoXu0V8iJJ4kXx0a2U978-5XtpMA5fdXdo5UQIuB5_MzMMx8wa0WpEOdkvCTj1YA1rtJm_jfWcrU3oiwHmebtfAushqwRkgtVAoNac1EI5CBF1qizUBxYyYSyDhicma2HFBwzJTo4C1eBqxDIJH1F-UO4aoVZU25Vockkjajg2hMmJF2RdBHff1SoQAp19BduwSN5kEJLqc_-OzRGGzLsjpNJ2r3C4z0Zrj2NO8mBDBfxcPh0Zlbc2dYard3d_TRJCd1cf0foRoqMjB8_PEsYoQ9v4eOdx0zyRBM682CN4lgIhRxsW2da-u-kLhcvW_sprfi-VVZwBF0AGS9vgMl4RehD5dzJes_ohtBNKVzVZEmua0I3j5WoT3bhdsg8Ufle352M_jfmnncmdRa9qe8idaHK6E98JHRjTU7o5vZSOvOhNQjCQmNZGah1GTs8bZdvzz-2z6vD_nXxuD4sXrY-a-dK5JU30Eq2YJvTSVvk4DQ0FmOmGpU7oZVH6WMySUEoYMC9ALrM6-IiohsFbKFi7wjOsPyIHLg-qwAsrG0QpNZBGizqItd1zRQHod51zsK9rmIOHr-xY3B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