------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2003-2014, 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 ();

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

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

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

   -------------
   -- 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");

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

   type Library_Kind is ("relocatable", "static");
   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 --
   -----------------------

   Root_Dir   := ".build";
   Target_Dir := Root_Dir & "/" & Target;

   for Exec_Dir use Target_Dir;

   case Build is
      when "Debug" =>
         for Exec_Dir use Project'Exec_Dir & "/debug/" & Library_Type;
      when "Release" =>
         for Exec_Dir use Project'Exec_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");
   ASIS   : Install_Status := external ("PRJ_ASIS", "Installed");
   LDAP   : Install_Status := external ("PRJ_LDAP", "Installed");

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

   Adaflags := External_As_List ("ADAFLAGS", " ");
   Cflags   := External_As_List ("CFLAGS", " ");
   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

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

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

   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;

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

      for Switches ("aws-attachments.adb")
        use Common_Options & Debug_Options & ("-gnatVn");
      for Switches ("aws-session.adb")
        use Common_Options & Debug_Options & ("-gnatVn");
      for Switches ("templates_parser.adb")
        use Common_Options & Debug_Options & ("-gnatVn");
      --  Disable validity check on this unit to work-around N728-028

      --  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;

   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;
   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;
