Asm code is not compiling under Xcode
I have added zstd library to my project as subdirectory using CMake. And when I configure and try to compile using Xcode it shows error in huff_decompress asm code. Problem appeared from 1.5.4 release version.
Steps to reproduce the behavior:
- Add zstd as subdirectory in CMake
- Configure Xcode project (cmake -G Xcode...)
- Open configured .xcodeproj and start program compilation
- See error in huf_decompress_amd64.S "Expected identifier or '('" at line 37
Expected behavior: Program compiling without errors.
Desktop:
- OS: macOS
- Version Ventura 13.3.1
- Compiler clang Xcode 14.3
- Flags -O0
- Build system CMake
This is caused by an assembler directive being outside the ifdef. Moving it inside the ifdef will fix the problem. (Change is trivial.) See https://gnats.netbsd.org/57383
This is caused by an assembler directive being outside the ifdef. Moving it inside the ifdef will fix the problem.
The statement is :
/* Stack marking
* ref: https://wiki.gentoo.org/wiki/Hardened/GNU_stack_quickstart
*/
#if defined(__ELF__) && defined(__GNUC__)
.section .note.GNU-stack,"",%progbits
#endif
This statement is meant to avoid executable stacks, which is considered harmful in certain security contexts.
It must be set, even when the target architecture is not x64, and there is consequently no ASM code to compile.
This is a complex situation, where some environments require this statement, while others do not.
Note that the statement is not unconditional : #if defined(__ELF__) && defined(__GNUC__).
So a strategy could be to extend the nb of conditions to validate, in order to exclude specific environments that do not like this statement.
cc @terrelln.
The reason the build fails (on my machine) is that Xcode adds a -x c to the clang command line.
It does that because CMake adds the following line to the Xcode project:
/* .../zstd-1.5.5/lib/decompress/huf_decompress_amd64.S */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.c; fileEncoding = 4; name = huf_decompress_amd64.S; path = ".../zstd-1.5.5/lib/decompress/huf_decompress_amd64.S"; sourceTree = SOURCE_ROOT; };
The explicitFileType = sourcecode.c.c is the problem. It should be sourcecode.asm and CMake is supposed to select it for assembly files:
https://github.com/Kitware/CMake/blob/dabba15172012f4497c96eadbf69684fc2f597ee/Source/cmGlobalXCodeGenerator.cxx#L1136-L1137
Now I wonder why huf_decompress_amd64.S is not recognized as "Assembly". The innermost project call explicitly enables the ASM language.
I'll try to dig deeper in the next days.
This gets broken by https://github.com/facebook/zstd/blob/5059618295bc67f4f70eb6f12e6cf57b8d3de141/build/cmake/lib/CMakeLists.txt#L104-L108
introduced by @terrelln in 651a38106 for #3193
My proposal to fix it is in #3665.
This doesn't need reopening, but I hit the exact same issue compiling with the (proprietary) Sony PS5 tool chain. Commenting out the set_source_files_properties line fixes the build (thanks for pointing this out @gjasny), but I don't understand the the build well enough to suggest a long-term fix.
(NB the source comment says this will disable the use of assembler)