Skip to content

Zig c++ does not respect -x flag #10915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
WardBrian opened this issue Feb 17, 2022 · 9 comments · Fixed by #13544
Closed

Zig c++ does not respect -x flag #10915

WardBrian opened this issue Feb 17, 2022 · 9 comments · Fixed by #13544
Labels
bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. zig cc Zig as a drop-in C compiler feature
Milestone

Comments

@WardBrian
Copy link

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 WardBrian added the bug Observed behavior contradicts documented or intended behavior label Feb 17, 2022
@Vexu Vexu added frontend Tokenization, parsing, AstGen, Sema, and Liveness. zig cc Zig as a drop-in C compiler feature labels Feb 17, 2022
@Vexu Vexu added this to the 0.10.0 milestone Feb 17, 2022
@kassane
Copy link
Contributor

kassane commented Feb 17, 2022

The zig did not recognize these extensions. hpp or foo

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

@WardBrian
Copy link
Author

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

@uhthomas
Copy link

Ran into this today, really confusing.

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

if (ext == .cpp) {

Could support be added for common c++ extensions?

@uhthomas
Copy link

Looks like there has been some groundwork for this here:

pub fn hasCppExt(filename: []const u8) bool {
, but other code like the previously linked would need to use this. (and add '.c++').

@uhthomas
Copy link

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

@andrewrk andrewrk added the enhancement Solving this issue will likely involve adding new logic or components to the codebase. label Aug 26, 2022
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Aug 26, 2022
@WardBrian
Copy link
Author

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

motiejus added a commit to motiejus/zig that referenced this issue Nov 14, 2022
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
motiejus added a commit to motiejus/zig that referenced this issue Dec 12, 2022
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
@sandhose
Copy link

sandhose commented Dec 16, 2022

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".

motiejus added a commit to motiejus/zig that referenced this issue Dec 16, 2022
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
@motiejus
Copy link
Contributor

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.

@sandhose
Copy link

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

motiejus added a commit to motiejus/zig that referenced this issue Dec 19, 2022
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
motiejus added a commit to motiejus/zig that referenced this issue Dec 19, 2022
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
motiejus added a commit to motiejus/zig that referenced this issue Jan 12, 2023
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
motiejus added a commit to motiejus/zig that referenced this issue Jan 12, 2023
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
motiejus added a commit to motiejus/zig that referenced this issue Jan 13, 2023
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes ziglang#10915
andrewrk pushed a commit that referenced this issue Jan 14, 2023
This commit adds support for "-x language" for a couple of hand-picked
supported languages. There is no reason the list of supported languages
to not grow (e.g. add "c-header"), but I'd like to keep it small at the
start.

Alternative 1
-------------

I first tried to add a new type "Language", and then add that to the
`CSourceFile`. But oh boy what a change it turns out to be. So I am
keeping myself tied to FileExt and see what you folks think.

Alternative 2
-------------

I tried adding `Language: ?[]const u8` to `CSourceFile`. However, the
language/ext, whatever we want to call it, still needs to be interpreted
in the main loop: one kind of handling for source files, other kind of
handling for everything else.

Test case
---------

*standalone.c*

    #include <iostream>

    int main() {
        std::cout << "elho\n";
    }

Compile and run:

    $ ./zig run -x c++ -lc++ standalone.c
    elho
    $ ./zig c++ -x c++ standalone.c -o standalone && ./standalone
    elho

Fixes #10915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. zig cc Zig as a drop-in C compiler feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants