3

I set out to rewrite an OSS project in C piece by piece to Ada. First stop being to replace the build system with GPR. On doing so I stumbled upon a problem: it does not allow for multiple sources in the repository with the same name:

duplicate source file name "slp_debug.h"

The file exists in two different directories; one for implementation, and one for test (stub I reckon). This should be just fine since the preprocessor will deterministically choose the source relative the source including it, or according to the order of include directories. But GPR would appear not fond of that idea.

How to make GPR accept it?

Andreas
  • 5,086
  • 3
  • 16
  • 36
  • C preprocessors do not choose among files. They simply include the file specified. Also the preprocessor has nothing to do with the linker. Try translating the C code into a set of Ada packages so that the identifiers can be distinguished as being in different scopes. – Jim Rogers Apr 21 '22 at 20:49
  • @JimRogers Explanation: `#include "foo.h"` will "choose" the foo.h file relative the including file if it exists, then search each include path `-I` in the order provided, then search in compiler internal paths. Not sure what the linker has to do with anything, the problem is in GPR and appears before both linking and compiling. Yes, making separate Ada packages should be a workaround. – Andreas Apr 22 '22 at 06:57
  • On second thought I don't think GPR may accept it. All object files will end up in the same folder, meaning any source name collision simply won't work. Accepting Simon's answer as it is a full display of a feasible alternative solution. – Andreas Apr 23 '22 at 09:25

1 Answers1

3

I had a go with openslp-2.0.0 and managed to make one GPR that builds the library and another, which withs the first, to build the tests - no complaints about the duplicate file.

Both project files are at the top level.

As common for projects like openslp, the configure process generates a lengthy config.h, at the top level, with a shedload of #define HAVE_FOO 1’s.

This is the 'common' project file - I think there may also be an executable in there (slptool), which might have to be a separate GPR since this one builds a library:

project Openslp is

   for Languages use ("c");

   for Source_Dirs use ("common", "libslp", "libslpattr");
   for Excluded_Source_Files use
     (
      "slp_win32.c",       -- not on Darwin! or Linux, ofc
      "libslpattr_tiny.c"  -- I think this must be an alternate version
     );

   for Library_Name use "slp";
   for Library_Kind use "static";
   for Library_Dir use "lib";

   for Object_Dir use "obj-openslp";
   for Create_Missing_Dirs use "true";

   package Compiler is
      for Switches ("c") use
        (
         "-DDARWIN",
         "-DHAVE_CONFIG_H",
         "-I" & project'Project_Dir,  -- for config.h, created by configure
         "-I" & project'Project_Dir & "/common",
         "-I" & project'Project_Dir & "/libslp",
         "-I" & project'Project_Dir & "/libslpattr",
         "-DETCDIR=""" & project'Project_Dir & "/etc"""
        );
   end Compiler;

end Openslp;

and this is the 'test' one:

with "openslp";
project Openslp_Test is

   for Languages use ("c");

   for Source_Dirs use ("test/**");
   for Object_Dir use "obj-openslp_test";

   for Exec_Dir use "test/bin";
   for Main use ("SLPFindAttrs.c");  -- etc, etc

   for Create_Missing_Dirs use "true";

   package Compiler is
      for Switches ("c") use
        ("-I" & project'Project_Dir & "/test")  -- for the local slp_debug.h
        & Openslp.Compiler'Switches ("c");
   end Compiler;

end Openslp_Test;
Simon Wright
  • 25,108
  • 2
  • 35
  • 62
  • I'd very much appreciate a display of the project file! – Andreas Apr 22 '22 at 19:42
  • Hope you have fun! – Simon Wright Apr 22 '22 at 22:10
  • GPR `with`'s rock ! And in Ada, you would be able to specify the list of each GPR library interface files (`.ads`), which, if I recall correctly, would prevent projects using the GPR lib from trying to `with` anything else : total control over interfaces/code visibility. – LoneWanderer May 12 '22 at 23:05