Skip to content

Commit fa64f21

Browse files
committed
Merge branch 'leo/178-pos_static_lib' into 'master'
Integrated instrumentation: recognize positional static libraries on command line Closes #178 See merge request eng/das/cov/gnatcoverage!359 gnatcov only used to recognize static libraries that were specified with the -l switch on link command lines. This change also makes it recognize \*.a files that are passed as positional arguments on the command line. Fixes eng/das/cov/gnatcoverage#178
2 parents 5ea4490 + d321363 commit fa64f21

File tree

7 files changed

+117
-12
lines changed

7 files changed

+117
-12
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project(HelloWorld)
2+
cmake_minimum_required(VERSION 3.0)
3+
4+
# Create a static library from the sources in src
5+
add_library(my_lib STATIC src/lib.c)
6+
7+
# Create an executable using the library previously declared
8+
add_executable(hello_world src/main.c)
9+
target_link_libraries(hello_world my_lib)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "lib.h"
2+
3+
int
4+
identity (int x)
5+
{
6+
return x;
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef LIB_H_
2+
#define LIB_H_
3+
4+
int identity (int x);
5+
6+
#endif // LIB_H_
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
3+
#include "lib.h"
4+
5+
int
6+
main ()
7+
{
8+
printf ("Hello world!\n");
9+
return identity (0);
10+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
Test that gnatcov correctly takes into account instrumented units present
3+
in a static library passed as a positional argument on the link command line.
4+
This is what is done by CMake when defining a library, then using it in an
5+
executable in the same CMakeLists.txt.
6+
"""
7+
8+
import os
9+
import os.path
10+
11+
from SUITE.control import env
12+
from SUITE.cutils import Wdir
13+
from SCOV.minicheck import check_xcov_reports
14+
from SUITE.tutils import cmdrun, srctracename_for, thistest, xcov
15+
16+
Wdir("tmp_")
17+
18+
cwd = os.getcwd()
19+
20+
# Setup the instrumentation process
21+
xcov(
22+
[
23+
"setup-integration",
24+
f"--files={os.path.join(cwd, '..', 'src', 'main.c')}",
25+
f"--files={os.path.join(cwd, '..', 'src', 'lib.c')}",
26+
"--compilers=gcc",
27+
]
28+
)
29+
30+
# Shadow the compiler driver with the generated wrapper
31+
env.add_search_path(env_var="PATH", path=cwd)
32+
33+
# Then, run the build process unchanged
34+
compiler_wrapper = os.path.join(cwd, 'gcc')
35+
cmdrun(["cmake", "..", f"-DCMAKE_C_COMPILER={compiler_wrapper}"], for_pgm=False)
36+
cmdrun(["make"], for_pgm=False)
37+
38+
# Run the executable
39+
cmdrun(["hello_world"], for_pgm=True)
40+
41+
# Check coverage expectations
42+
xcov(
43+
[
44+
"coverage",
45+
"--level=stmt",
46+
"--sid=main.c.sid",
47+
"--sid=lib.c.sid",
48+
"-axcov",
49+
srctracename_for("main"),
50+
]
51+
)
52+
check_xcov_reports(
53+
"*.xcov",
54+
{
55+
"main.c.xcov": {"+": {8, 9}},
56+
"lib.c.xcov": {"+": {6}},
57+
}
58+
)
59+
60+
thistest.result()

tools/gnatcov/command_line.ads

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ package Command_Line is
12961296
| Cmd_Instrument_Main
12971297
| Cmd_Setup_Integration => True,
12981298
others => False),
1299-
Internal => True),
1299+
Internal => False),
13001300

13011301
Opt_Shared_Object => Create
13021302
(Long_Name => "--shared-object",

tools/gnatcov/compiler_wrappers-gcc.adb

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ procedure Compiler_Wrappers.Gcc is
519519
else
520520
Result.Object_Files.Append (Arg);
521521
end if;
522+
elsif Ends_With (Arg, ".a") then
523+
Result.Libraries.Append (Arg);
522524
end if;
523525
end;
524526
end loop;
@@ -631,17 +633,28 @@ procedure Compiler_Wrappers.Gcc is
631633
Ada.Environment_Variables.Value ("LIBRARY_PATH", ""));
632634

633635
for Library of Command.Libraries loop
634-
declare
635-
use type GNAT.OS_Lib.String_Access;
636-
Library_File : GNAT.OS_Lib.String_Access :=
637-
GNAT.OS_Lib.Locate_Regular_File
638-
("lib" & (+Library) & ".a", +Library_Path);
639-
begin
640-
if Library_File /= null then
641-
Result.Union (Coverage_Buffer_Symbols (Library_File.all));
642-
GNAT.OS_Lib.Free (Library_File);
643-
end if;
644-
end;
636+
637+
if Ends_With (Library, ".a") then
638+
639+
-- Library filename on the command line, no need to look it up
640+
641+
Result.Union (Coverage_Buffer_Symbols (+Library));
642+
else
643+
-- Simple library name passed to the -l option, search the
644+
-- actual file on the library path.
645+
646+
declare
647+
use type GNAT.OS_Lib.String_Access;
648+
Library_File : GNAT.OS_Lib.String_Access :=
649+
GNAT.OS_Lib.Locate_Regular_File
650+
("lib" & (+Library) & ".a", +Library_Path);
651+
begin
652+
if Library_File /= null then
653+
Result.Union (Coverage_Buffer_Symbols (Library_File.all));
654+
GNAT.OS_Lib.Free (Library_File);
655+
end if;
656+
end;
657+
end if;
645658
end loop;
646659
end;
647660

0 commit comments

Comments
 (0)