Skip to content

Missing static library (compiled as custom_target) in dependencies in ninja.build #12006

Closed

Description

@woju

Describe the bug
Static library compiled as custom_target() is missing from dependencies.

Reproduced on current master 7824ff8
Bisected to a78af23

To Reproduce
meson.build:

project('repro', 'c')

s1 = custom_target('s1',
    command: [
        find_program('compile.sh'),
        '@CURRENT_SOURCE_DIR@',
        meson.current_build_dir(),
        '@PRIVATE_DIR@',
    ],
    output: 'libs1.a',
)

s2 = static_library('s2',
    's2.c',
    link_with: s1,
    install: true,
)

main = executable('main',
    'main.c',
    link_with: s2,
)

s1.c:

int s1(void)
{
    return 42;
}

s2.c:

int s1(void);

int s2(void)
{
    return s1();
}

main.c:

int s2(void);

int main(int argc, char *argv[])
{
    return s2();
}

compile.sh:

#!/bin/sh

CURRENT_SOURCE_DIR=$1
CURRENT_BUILD_DIR=$2
PRIVATE_DIR=$3

mkdir -p "$PRIVATE_DIR"
cc -c -o "$PRIVATE_DIR"/s1.o "$CURRENT_SOURCE_DIR"/s1.c
ar rcs "$CURRENT_BUILD_DIR"/libs1.a "$PRIVATE_DIR"/s1.o

Expected behaviour

[1/5] ccache cc -Ilibs2.a.p -I. -I../../../testmesonstaticlink -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -fPIC -MD -MQ libs2.a.p/s2.c.o -MF libs2.a.p/s2.c.o.d -o libs2.a.p/s2.c.o -c ../../../testmesonstaticlink/s2.c
[2/5] ccache cc -Imain.p -I. -I../../../testmesonstaticlink -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -MD -MQ main.p/main.c.o -MF main.p/main.c.o.d -o main.p/main.c.o -c ../../../testmesonstaticlink/main.c
[3/5] rm -f libs2.a && gcc-ar csrD libs2.a libs2.a.p/s2.c.o
[4/5] /home/woju/testmesonstaticlink/compile.sh ../../../testmesonstaticlink/ /home/woju/src/meson/build-test libs1.a.p
[5/5] cc  -o main main.p/main.c.o -Wl,--as-needed -Wl,--no-undefined '-Wl,-rpath,$ORIGIN/' -Wl,-rpath-link,/home/woju/src/meson/build-test/ -Wl,--start-group libs2.a libs1.a -Wl,--end-group

Actual behaviour

[1/4] ccache cc -Ilibs2.a.p -I. -I../../../testmesonstaticlink -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -fPIC -MD -MQ libs2.a.p/s2.c.o -MF libs2.a.p/s2.c.o.d -o libs2.a.p/s2.c.o -c ../../../testmesonstaticlink/s2.c
[2/4] ccache cc -Imain.p -I. -I../../../testmesonstaticlink -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g -MD -MQ main.p/main.c.o -MF main.p/main.c.o.d -o main.p/main.c.o -c ../../../testmesonstaticlink/main.c
[3/4] rm -f libs2.a && gcc-ar csrD libs2.a libs2.a.p/s2.c.o
[4/4] cc  -o main main.p/main.c.o -Wl,--as-needed -Wl,--no-undefined '-Wl,-rpath,$ORIGIN/' -Wl,-rpath-link,/home/woju/src/meson/build-test/ -Wl,--start-group libs2.a -Wl,--end-group
FAILED: main 
cc  -o main main.p/main.c.o -Wl,--as-needed -Wl,--no-undefined '-Wl,-rpath,$ORIGIN/' -Wl,-rpath-link,/home/woju/src/meson/build-test/ -Wl,--start-group libs2.a -Wl,--end-group
/usr/bin/ld: libs2.a(s2.c.o): in function `s2':
/home/woju/src/meson/build-test/../../../testmesonstaticlink/s2.c:5: undefined reference to `s1'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Difference between ninja.build files:

--- right/build.ninja	2023-07-18 17:40:12.503267754 +0200
+++ wrong/build.ninja	2023-07-18 17:40:04.743527324 +0200
@@ -59,8 +59,8 @@
  DEPFILE_UNQUOTED = main.p/main.c.o.d
  ARGS = -Imain.p -I. -I../../../testmesonstaticlink -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g
 
-build main: c_LINKER main.p/main.c.o | libs1.a libs2.a
- LINK_ARGS = -Wl,--as-needed -Wl,--no-undefined '-Wl,-rpath,$$ORIGIN/' -Wl,-rpath-link,/home/woju/src/meson/build-test/ -Wl,--start-group libs2.a libs1.a -Wl,--end-group
+build main: c_LINKER main.p/main.c.o | libs2.a
+ LINK_ARGS = -Wl,--as-needed -Wl,--no-undefined '-Wl,-rpath,$$ORIGIN/' -Wl,-rpath-link,/home/woju/src/meson/build-test/ -Wl,--start-group libs2.a -Wl,--end-group
 
 # Test rules
 

acknowledgement

Originally reported by @anjalirai-intel as gramineproject/gramine#1459

Activity

  1. xclaesse commented on Jul 18, 2023

    @xclaesse
    Member

    The expected behaviour is a setup time error. s2 is installed but not s1, that means s2 must contain object files from s1 which Meson cannot currently do. It would have to extract the archive, similar to what that PR wants to achieve: #9218.

  2. added 3 commits that reference this issue on Jul 19, 2023
    d57b5b7
    c2efa2e
    4bbbb32
  3. added 6 commits that reference this issue on Aug 2, 2023
    3539a29
    3bcf8fc
    1774a04
    526854b
    ac269dd
    8ece991
  4. added a commit that references this issue on Aug 7, 2023
    926c3a6
  5. added a commit that references this issue on Aug 7, 2023
    8f6fdb7
  6. added a commit that references this issue on Aug 25, 2023
    ebee9f5
  7. added a commit that references this issue on May 1, 2025
    6379389
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions