% Copyright 1996-2000 Critical Mass, Inc. All rights reserved.
% See file COPYRIGHT-CMASS for details.
%
% Standard configuration file for an x86 machine running Windows NT or perhaps Windows 95,
% uses the integrated backend, Win32 threads, Win32 gui, Microsoft C library.
%
% Most of the variables in this file are independent of each other and
% arbitrary configurations can be produced by changing them.

%-------------------------------------------------------------------

readonly TARGET = "NT386"   % code generation target
readonly TARGET_OS = "NT"
readonly OS_TYPE = "WIN32"

%-------------------------------------------------------------------
% defined by cm3, but not the other MxConfig users
if not defined("CR") CR = "\n" end
if not defined("EOL") EOL = "\n" end
if not defined("M3_PROFILING") M3_PROFILING = FALSE end
if not defined("SL") SL = "/" end
%-------------------------------------------------------------------

if M3_PROFILING
    readonly PROFILING_P = "p"
else
    readonly PROFILING_P = ""
end

readonly BUILD_DIR    = "NT386" & PROFILING_P

readonly M3_BACKEND_MODE = "0"
% -- defines how the frontend, backend, and assembler interact
%  "0"  -- don't call m3_backend, M3CG produces object code
%  "1"  -- don't call m3_backend, M3CG produces assembly code
%  "2"  -- call m3_backend, it produces object code
%  "3"  -- call m3_backend, it produces assembly code

% for bootstrapping from older tools
readonly NAMING_CONVENTIONS = "2"

% "MS" cl
% "GNU" gcc

readonly C_COMPILER = "MS"

% "MS" link
% "GNU" gcc/ld

readonly LINKER = "MS"

% "MS" Win32 kernel threads
% "PTHREADS" Cygwin pthreads
%
% This is currently ignored, but a reasonable idea.

readonly THREAD_LIBRARY = "MS"

% "MS"
% "X"

readonly WINDOW_LIBRARY = "MS"


% Do a good job of setting up Visual C++ environment.
% This is only for native builds.

proc SetupEnvironment() is

    local SystemDrive = $SystemDrive
    local HostIsNT = equal($OS, "Windows_NT")

    if HostIsNT and not equal(SystemDrive, "")
        SystemDrive = SystemDrive & "\\"

      % Do this earlier so that its link isn't a problem.
      % Looking in the registry HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2
      % would be reasonable here.

%     if HostIsCygwin:
%         _SetupEnvironmentVariableAll(
%             "PATH",
%             ["cygwin1.dll"],
%             os.path.join(SystemDrive, "cygwin", "bin"))

        local VCBin = ""
        local VCInc = ""
        local VCLib = ""
        local MspdbDir = ""

        % 4.0 e:\MSDEV
        % 5.0 E:\Program Files\DevStudio\SharedIDE
        local MSDevDir = $MSDevDir

        % 5.0
        local MSVCDir = $MSVCDir % E:\Program Files\DevStudio\VC

        % 7.1 Express
        local VCToolkitInstallDir = $VCToolkitInstallDir % E:\Program Files\Microsoft Visual C++ Toolkit 2003 (not set by vcvars32)

        % 8.0 Express
        % E:\Program Files\Microsoft Visual Studio 8\VC
        % E:\Program Files\Microsoft Visual Studio 8\Common7\Tools

        local DevEnvDir = $DevEnvDir % E:\Program Files\Microsoft Visual Studio 8\Common7\IDE
        local VSInstallDir = $VSINSTALLDIR % E:\Program Files\Microsoft Visual Studio 8
        local VS80CommonTools = $VS80COMNTOOLS % E:\Program Files\Microsoft Visual Studio 8\Common7\Tools
        local VCInstallDir = $VCINSTALLDIR % E:\Program Files\Microsoft Visual Studio 8\VC

        % 9.0 Express
        % always, global
        %VS90COMNTOOLS=D:\msdev\90\Common7\Tools\
        % after running the shortcut
        %VCINSTALLDIR=D:\msdev\90\VC
        %VSINSTALLDIR=D:\msdev\90
        %
        % The Windows SDK is carried with the express edition and tricky to find.
        % Best if folks just run the installed shortcut probably.

        % This is not yet finished.
        %
        % Probe the partly version-specific less-polluting environment variables,
        % from newest to oldest.
        % That is, having setup alter PATH, INCLUDE, and LIB system-wide is not
        % a great idea, but having setup set DevEnvDir, VSINSTALLDIR, VS80COMNTOOLS, etc.
        % isn't so bad and we can temporarily establish the first set from the second
        % set.

        if VSInstallDir

            % Visual C++ 2005/8.0, at least the Express Edition, free download

            if not VCInstallDir
                VCInstallDir = VSInstallDir & "\\VC"
            end

            if not DevEnvDir
                DevEnvDir = VSInstallDir & "\\Common7\\IDE"
            end

            MspdbDir = DevEnvDir

        else if VCToolkitInstallDir

            % free download Visual C++ 2003; no longer available

            VCInstallDir = VCToolkitInstallDir

        else if MSVCDir and MSDevDir

            % Visual C++ 5.0
            %
            % do more research
            % VCInstallDir = MSVCDir

        else if MSDevDir

            % Visual C++ 4.0, 5.0
            %
            % do more research
            % VCInstallDir = MSDevDir

        else

            % This is what really happens on my machine, for 8.0.
            % It might be good to guide pylib.py to other versions,
            % however setting things up manually suffices and I have, um,
            % well automated.

            Msdev = SystemDrive & "\\msdev\\80"
            VCInstallDir = Msdev & "\\VC"
            DevEnvDir = Msdev & "\\Common7\\\IDE"

        end end end end

        if VCInstallDir
            VCBin = VCInstallDir & "\\bin"
            VCLib = VCInstallDir & "\\lib"
            VCInc = VCInstallDir & "\\include"
        end

        if DevEnvDir
            MspdbDir = DevEnvDir
        %else if VCBin
        %    MspdbDir = VCBin
        end

%         if _CheckSetupEnvironmentVariableAll("INCLUDE", ["errno.h", "windows.h"], VCInc)
%             a = os.path.join(SystemDrive, "Program Files")
%             b = os.path.join(a, "Microsoft Platform SDK for Windows Server 2003 R2", "Include")
%             c = os.path.join(a, "Microsoft SDKs", "Windows", "v6.0A", "Include")
%             _SetupEnvironmentVariableAll("INCLUDE", ["errno.h", "windows.h"], VCInc + ";" + c + ";" + b)

%         _SetupEnvironmentVariableAll(
%             "LIB",
%             ["kernel32.lib", "libcmt.lib"],
%             VCLib + os.path.pathsep + os.path.join(InstallRoot, "lib"))

%         # Do this before mspdb*dll because it sometimes gets it in the path.

%         _SetupEnvironmentVariableAll("PATH", ["cl", "link"], VCBin)

%         _SetupEnvironmentVariableAny(
%             "PATH",
%             ["mspdb80.dll", "mspdb71.dll", "mspdb70.dll", "mspdb60.dll", "mspdb50.dll", "mspdb41.dll", "mspdb40.dll", "dbi.dll"],
%             MspdbDir)

%         _SetupEnvironmentVariableAny(
%             "PATH",
%             ["msobj80.dll", "msobj71.dll", "msobj10.dll", "msobj10.dll", "mspdb50.dll", "mspdb41.dll", "mspdb40.dll", "dbi.dll"],
%             MspdbDir)

%         # The free Visual C++ 2003 has neither delayimp.lib nor msvcrt.lib.
%         # Very old toolsets have no delayimp.lib.
%         # The Quake config file checks these environment variables.

%         Lib = os.environ.get("LIB")
%         if not SearchPath("delayimp.lib", Lib)
%             os.environ["USE_DELAYLOAD"] = "0"
%             print("set USE_DELAYLOAD=0")

%         if not SearchPath("msvcrt.lib", Lib)
%             os.environ["USE_MSVCRT"] = "0"
%             print("set USE_MSVCRT=0")
%         end
    end
end

SetupEnvironment()

include("NT386.common")
