zig icon indicating copy to clipboard operation
zig copied to clipboard

Zig c++ does not respect -x flag

Open WardBrian opened this issue 3 years ago • 6 comments

Zig Version

0.10.0-dev.760+1a84c23d6

Steps to Reproduce

Save the following to a file with an extension which is not generally used for C++, such as test.foo or test.hpp:

// test

int main(void){
  return 1;
}

Compile with

$ zig c++ -x c++ test.foo

Expected Behavior

If the file is named test.cpp, it compiles just fine. I would expect that -x c++ to emulate this behavior.

Actual Behavior

The zig c++ command fails:

ld.lld: error: test.foo:1: unknown directive: //
>>> // test
>>> ^

WardBrian avatar Feb 17 '22 15:02 WardBrian

The zig did not recognize these extensions. hpp or foo

https://github.com/ziglang/zig/blob/master/src/Compilation.zig#L4170-L4201

kassane avatar Feb 17 '22 19:02 kassane

Correct. Maybe I should have noted that gcc/clang do not exhibit this issue, there if -x c++ is specified the file can have any extension (or none at all) and be treated as a .cpp would

WardBrian avatar Feb 17 '22 20:02 WardBrian

Ran into this today, really confusing.

capnproto uses the 'c++' file extension, which is looks like zig doesn't support.

https://github.com/ziglang/zig/blob/1fee9eac8bb5d2e3e78c098b9cebe2cda332e7cf/src/Compilation.zig#L3938

Could support be added for common c++ extensions?

uhthomas avatar Jul 13 '22 19:07 uhthomas

Looks like there has been some groundwork for this here: https://github.com/ziglang/zig/blob/1fee9eac8bb5d2e3e78c098b9cebe2cda332e7cf/src/Compilation.zig#L4349, but other code like the previously linked would need to use this. (and add '.c++').

uhthomas avatar Jul 13 '22 19:07 uhthomas

Bazel has a nice list of extensions: https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library

Permitted srcs file types:

C and C++ source files: .c, .cc, .cpp, .cxx, .c++, .C C and C++ header files: .h, .hh, .hpp, .hxx, .inc, .inl, .H Assembler with C preprocessor: .S Archive: .a, .pic.a "Always link" library: .lo, .pic.lo Shared library, versioned or unversioned: .so, .so.version Object file: .o, .pic.o

uhthomas avatar Jul 13 '22 19:07 uhthomas

Bazel has a nice list of extensions

To clarify, while a partial solution to this is to expand the list of automatically recognized extensions, that is sort of orthogonal to the immediate issue of the -x flag. This is supposed to treat the file as the specified language even if it has a contradictory extension, or an extension nobody has ever seen before

WardBrian avatar Nov 03 '22 19:11 WardBrian

I think I'm hitting this issue (at least a similar one) when trying to build the psm Rust crate with cargo-zigbuild. The former has assembly for each arch, and the latter is just a wrapper around zig cc to use with rustc.

It fails to build src/arch/aarch_aapcs64.s with the following flags:

zig cc
  -target aarch64-linux-gnu
  -g
  -O0
  -ffunction-sections
  -fdata-sections 
  -fPIC 
  -gdwarf-4 
  -fno-omit-frame-pointer 
  -Wall
  -Wextra
  -xassembler-with-cpp
  -DCFG_TARGET_OS_linux
  -DCFG_TARGET_ARCH_aarch64
  -DCFG_TARGET_ENV_gnu
  -o /tmp/aarch_aapcs64.o
  -c src/arch/aarch_aapcs64.s
It fails with the following logs: (click to expand)
zig: warning: '-x assembler-with-cpp' after last input file has no effect [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-ffunction-sections' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-D __GLIBC_MINOR__=19' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-fdata-sections' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-D CFG_TARGET_OS_linux' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-D CFG_TARGET_ARCH_aarch64' [-Wunused-command-line-argument]
zig: warning: argument unused during compilation: '-D CFG_TARGET_ENV_gnu' [-Wunused-command-line-argument]
src/arch/aarch_aapcs64.s:29:1: error: unrecognized instruction mnemonic
GLOBL(rust_psm_stack_direction)
^
src/arch/aarch_aapcs64.s:31:1: error: unrecognized instruction mnemonic
TYPE(rust_psm_stack_direction)
^
src/arch/aarch_aapcs64.s:32:35: error: unexpected token in argument list
FUNCTION(rust_psm_stack_direction):
                                  ^
src/arch/aarch_aapcs64.s:33:1: error: unexpected token at start of statement
/* extern "C" fn() -> u8 */
^
src/arch/aarch_aapcs64.s:35:18: error: expected compatible register or logical immediate
    orr w0, wzr, #STACK_DIRECTION_DESCENDING
                 ^
src/arch/aarch_aapcs64.s:38:30: error: expected ')'
SIZE(rust_psm_stack_direction,.rust_psm_stack_direction_end)
                             ^
src/arch/aarch_aapcs64.s:38:5: error: invalid operand
SIZE(rust_psm_stack_direction,.rust_psm_stack_direction_end)
    ^
src/arch/aarch_aapcs64.s:42:1: error: unrecognized instruction mnemonic
GLOBL(rust_psm_stack_pointer)
^
src/arch/aarch_aapcs64.s:44:1: error: unrecognized instruction mnemonic
TYPE(rust_psm_stack_pointer)
^
src/arch/aarch_aapcs64.s:45:33: error: unexpected token in argument list
FUNCTION(rust_psm_stack_pointer):
                                ^
src/arch/aarch_aapcs64.s:46:1: error: unexpected token at start of statement
/* extern "C" fn() -> *mut u8 */
^
src/arch/aarch_aapcs64.s:51:28: error: expected ')'
SIZE(rust_psm_stack_pointer,.rust_psm_stack_pointer_end)
                           ^
src/arch/aarch_aapcs64.s:51:5: error: invalid operand
SIZE(rust_psm_stack_pointer,.rust_psm_stack_pointer_end)
    ^
src/arch/aarch_aapcs64.s:55:1: error: unrecognized instruction mnemonic
GLOBL(rust_psm_replace_stack)
^
src/arch/aarch_aapcs64.s:57:1: error: unrecognized instruction mnemonic
TYPE(rust_psm_replace_stack)
^
src/arch/aarch_aapcs64.s:58:33: error: unexpected token in argument list
FUNCTION(rust_psm_replace_stack):
                                ^
src/arch/aarch_aapcs64.s:59:1: error: unexpected token at start of statement
/* extern "C" fn(r0: usize, r1: extern "C" fn(usize), r2: *mut u8) */
^
src/arch/aarch_aapcs64.s:65:28: error: expected ')'
SIZE(rust_psm_replace_stack,.rust_psm_replace_stack_end)
                           ^
src/arch/aarch_aapcs64.s:65:5: error: invalid operand
SIZE(rust_psm_replace_stack,.rust_psm_replace_stack_end)
    ^
src/arch/aarch_aapcs64.s:69:1: error: unrecognized instruction mnemonic
GLOBL(rust_psm_on_stack)
^
src/arch/aarch_aapcs64.s:71:1: error: unrecognized instruction mnemonic
TYPE(rust_psm_on_stack)
^
src/arch/aarch_aapcs64.s:72:28: error: unexpected token in argument list
FUNCTION(rust_psm_on_stack):
                           ^
src/arch/aarch_aapcs64.s:73:1: error: unexpected token at start of statement
/* extern "C" fn(r0: usize, r1: usize, r2: extern "C" fn(usize, usize), r3: *mut u8) */
^
src/arch/aarch_aapcs64.s:91:23: error: expected ')'
SIZE(rust_psm_on_stack,.rust_psm_on_stack_end)
                      ^
src/arch/aarch_aapcs64.s:91:5: error: invalid operand
SIZE(rust_psm_on_stack,.rust_psm_on_stack_end)
    ^

It worked well with zig 0.9.1, but broke with 0.10.0. Interestingly, renaming the file (mv src/arch/aarch_aapcs64.s src/arch/aarch_aapcs64.S) makes it work, but with the same warning about the "-x after last input".

sandhose avatar Dec 16 '22 10:12 sandhose

It worked well with zig 0.9.1, but broke with 0.10.0. Interestingly, renaming the file (mv src/arch/aarch_aapcs64.s src/arch/aarch_aapcs64.S) makes it work, but with the same warning about the "-x after last input".

Can you try with #13544? I just added it to understand assembler-with-cpp.

motiejus avatar Dec 16 '22 12:12 motiejus

It worked well with zig 0.9.1, but broke with 0.10.0. Interestingly, renaming the file (mv src/arch/aarch_aapcs64.s src/arch/aarch_aapcs64.S) makes it work, but with the same warning about the "-x after last input".

Can you try with #13544? I just added it to understand assembler-with-cpp.

@motiejus I built your PR and tried ; it fails with the following error:

$ /tmp/zig/build/stage3/bin/zig cc -target aarch64-linux-gnu -g -O0 -ffunction-sections -fdata-sections -fPIC -gdwarf-4 -fno-omit-frame
-pointer -Wall -Wextra -xassembler-with-cpp -DCFG_TARGET_OS_linux -DCFG_TARGET_ARCH_aarch64 -DCFG_TARGET_ENV_gnu -o /tmp/aarch_aapcs64.o -c src/arch/aar
ch_aapcs64.s
error: language 'assembly' is unsupported in this context

To reproduce:

git clone https://github.com/rust-lang/stacker
cd stacker/psm
zig cc -target aarch64-linux-gnu -g -O0 -ffunction-sections -fdata-sections -fPIC -gdwarf-4 -fno-omit-frame-pointer -Wall -Wextra -xassembler-with-cpp -DCFG_TARGET_OS_linux -DCFG_TARGET_ARCH_aarch64 -DCFG_TARGET_ENV_gnu -o /tmp/aarch_aapcs64.o -c src/arch/aarch_aapcs64.s

sandhose avatar Dec 16 '22 16:12 sandhose