------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2003-2023, AdaCore                     --
--                                                                          --
--  This is free software;  you can redistribute it  and/or modify it       --
--  under terms of the  GNU General Public License as published  by the     --
--  Free Software  Foundation;  either version 3,  or (at your option) any  --
--  later version.  This software is distributed in the hope  that it will  --
--  be useful, but WITHOUT ANY WARRANTY;  without even the implied warranty --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     --
--  General Public License for  more details.                               --
--                                                                          --
--  You should have  received  a copy of the GNU General  Public  License   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

abstract project Shared is

   for Source_Dirs use ();

   Build_Target_Dir := external("TGT_DIR");

   type Build_Type is ("Debug", "Release");
   Build : Build_Type := external ("PRJ_BUILD", "Debug");

   type Install_Status is ("Installed", "Disabled");

   Processors := External ("PROCESSORS", "0");

   type Boolean is ("true", "false");

   Thread_Sanitizer : Boolean := External ("THREAD_SANITIZER", "false");

   -------------
   -- SOCKLIB --
   -------------

   type SOCKLIB_Type is ("gnat", "ipv6", "ipv4");
   SOCKLIB : SOCKLIB_Type := external ("PRJ_SOCKLIB", "gnat");

   ------------
   -- SOCKET --
   ------------

   type Socket_Type is ("std", "ssl", "openssl", "gnutls");
   Socket : Socket_Type := external ("SOCKET", "std");

   SSL_Dynamic : Boolean := External ("SSL_DYNAMIC", "false");

   --------------------------
   -- Static / Relocatable --
   --------------------------

   type Library_Kind is ("relocatable", "static", "static-pic");
   Library_Type : Library_Kind := external ("LIBRARY_TYPE", "static");

   ---------------------
   -- Target to build --
   ---------------------

   Target := external ("TARGET", "i686-pc-mingw32");

   type S_Target_type is ("Windows_NT", "UNIX", "Darwin", "vxworks", "freebsd");
   S_Target : S_Target_Type := external ("PRJ_TARGET");

   -----------------------
   -- Build directories --
   -----------------------

   Target_Dir := Target;

   case Build is
      when "Debug" =>
         for Exec_Dir use Target_Dir & "/debug/" & Library_Type;
      when "Release" =>
         for Exec_Dir use Target_Dir & "/release/" & Library_Type;
   end case;

   for Object_Dir use Project'Exec_Dir & "/obj";
   for Library_Dir use Project'Exec_Dir & "/lib";

   ------------------------
   -- External Libraries --
   ------------------------

   XMLAda : Install_Status := external ("PRJ_XMLADA", "Installed");
   LAL    : Install_Status := external ("PRJ_LAL", "Installed");
   LDAP   : Install_Status := external ("PRJ_LDAP", "Installed");

   --------------------
   -- External flags --
   --------------------

   Adaflags := External_As_List ("ADAFLAGS", " ");
   Cflags   := External_As_List ("CFLAGS", " ");
   Cppflags := External_As_List ("CPPFLAGS", " ");
   Ldflags  := External_As_List ("LDFLAGS", " ");

   ---------
   -- Ide --
   ---------

   type VCS_Type is ("Subversion", "Git");
   VCS_Kind : VCS_Type := external ("PRJ_VCS", "Git");

   package Ide is
      for VCS_Kind use VCS_Kind;
      for VCS_Log_Check
        use "style_checker -l70 -H";
      for VCS_File_Check
        use "style_checker -ign out -ign tmplt -ign sed -ign txt"
         & " -lang Ada -cp -cy -sp -gnat2012"
         & " -lang XML -l256";
   end Ide;

   --------------
   -- Compiler --
   --------------

   Global_Options := ();
   --  Options used for all Ada units in both Debug and Release modes

   Instrument_Options := ();
   --  Instrumentation option

   case Thread_Sanitizer is
      when "false" =>
         null;
      when "true" =>
         Instrument_Options := ("-fsanitize=thread");
   end case;

   Common_Options :=
     ("-gnat2012", "-gnatwcfijkmruv", "-gnaty3abBcdefhiIklmnoOprstx")
     & Global_Options & Instrument_Options;
   --  Common options used for the Debug and Release modes

   Debug_Options :=
     ("-g", "-gnata", "-gnatVa", "-gnatQ", "-gnato", "-Wall", "-gnatwe");

   Release_Options := ("-O2", "-gnatn");

   package Compiler is

      for Driver ("Makefile") use "";

      case Build is
         when "Debug" =>
            for Default_Switches ("Ada") use Common_Options & Debug_Options;
            for Default_Switches ("C")
              use ("-g", "-Wno-implicit-function-declaration");

         when "Release" =>
            for Default_Switches ("Ada") use Common_Options & Release_Options;
            for Default_Switches ("C")
              use ("-O2", "-Wno-implicit-function-declaration");
      end case;

      for Switches ("ssl-thin.ads")
        use Compiler'Default_Switches ("Ada") & "-gnatyM300";
      --  The file ssl-thin.ads is generated from ssl-thin__openssl.ads or from
      --  ssl-thin__gnutls.ads and was reformatted with gnatpp, but gnatpp
      --  unable to split long line of aspect list, see V117-043. It is because
      --  we use "-gnatyM300" option to increate maximum line length.

      case S_Target is
         when "Darwin" =>
            for Default_Switches ("Ada")
              use Compiler'Default_Switches ("Ada") & ("-fno-common");
         when others =>
            null;
      end case;

      --  ADAFLAGS and CFLAGS should come last so that command line
      --  settings override the ones in this project.

      for Default_Switches ("Ada") use
        Compiler'Default_Switches ("Ada") & Adaflags;
      for Default_Switches ("C") use
        Compiler'Default_Switches ("C") & Cflags & Cppflags;

   end Compiler;

   ------------
   -- Binder --
   ------------

   package Binder is
      for Default_Switches ("Ada") use ("-E");
   end Binder;

   -------------
   -- Builder --
   -------------

   package Builder is
      for Switches (others) use ("-m", "-j" & Processors);
   end Builder;

   ------------
   -- Linker --
   ------------

   package Linker is
      for Default_Switches ("Ada") use Ldflags & Instrument_Options;
   end Linker;

   --  LDFLAGS should come first so that command line settings
   --  influence the way -l options are handled afterwards.
   --  For library projects, use Leading_Library_Options instead.
   Leading_Library_Options := Ldflags;

   ------------
   -- Naming --
   ------------

   package Naming is
      for Implementation_Suffix ("Makefile") use ".txt";
      for Implementation_Exceptions ("Makefile") use ("Makefile");
   end Naming;

end Shared;
