classdef (Hidden=true) GmakeWriter < coder.make.MakefileWriter
    % Class that represents a gmake makefile writer.
    
    % Copyright 2012-2019 The MathWorks, Inc.
    
    properties
    end
    
    methods (Access = public)
        % -----------------------------------------------------------------
        % xxConstructor
        % -----------------------------------------------------------------
        function h = GmakeWriter(builder, buildInfo)
            % Call base class constructor
            h@coder.make.MakefileWriter(builder, buildInfo);
            
            % Initialize            
            h.IsArtifactWrapped = 0;
            % h.ArtifactWrapper   = [];
            h.addResponseFileToCompileFlags(buildInfo);
        end
    end
    
    methods
        
        % -------------------------------------------------------------
        % Remove the include paths from the compiler option string
        % -------------------------------------------------------------
        function addResponseFileToCompileFlags(h, buildInfo)
            
            % Determine if compiler command file is in use
            lUseCompilerCommandFile = coder.make.useCompilerCommandFile...
                (h.ToolchainInfo);
            if ~lUseCompilerCommandFile
                return
            end
            
            comps = {'C Compiler', 'C++ Compiler', 'MEX Compiler', 'MEX C++ Compiler'};
            for k=1:numel(comps)
                if h.Parent.Components.ToolchainFlags.isKey(comps{k})
                    cCompFlags = h.Parent.Components.ToolchainFlags.getValue(comps{k});
                    if isempty(cCompFlags.buildinfo)
                        cCompFlags.buildinfo.macro = regexprep([comps{k} '_BASIC'], {'+', ' '}, {'P', '_'});
                        cCompFlags.buildinfo.value = ' @$(COMPILER_COMMAND_FILE)';
                    else
                        macros = {cCompFlags.buildinfo.macro};
                        for i=1:numel(macros)
                            idx = strfind(macros{i}, '_BASIC');
                            if ~isempty(idx)
                                cCompFlags.buildinfo(i).value = [cCompFlags.buildinfo(i).value ' @$(COMPILER_COMMAND_FILE)'];
                            end
                        end
                    end
                    h.Parent.Components.ToolchainFlags.setValue(comps{k}, cCompFlags);
                end
            end
        end

        % -----------------------------------------------------------------
        % Get the commands for formulating the list of objects
        % -----------------------------------------------------------------
        function [macros, values] = getObjectCommands(h, buildInfo) %#ok<*INUSD>
            srcsMacro = h.getMacro('source files');
            objsMacro = h.getMacro('objects');
            
            % Object list with paths - just transform the source file list
            % to have .<object> extension
            macros{1}     = objsMacro;
            objs          = h.ToolchainInfo.getIOFileExtensions('output');
            values{1}     = sprintf('$(addsuffix %s, $(basename $(%s)))', ...
                            objs{1}, srcsMacro);
            % Object list with no paths
            macros{end+1} = h.getMacro('objects');
            values{end+1} = sprintf('$(notdir $(%s))', objsMacro);
        end
            
        % -----------------------------------------------------------------
        % Syntax:
        %   <ECHO> "<STRING>"
        % -----------------------------------------------------------------
        function out = formDisplayCommand(h, str)
            out = [h.DisplayDirective ' "' str '"'];
        end
        
        % -----------------------------------------------------------------
        % Print an in-place assignment
        % -----------------------------------------------------------------
        function printAssignmentInPlace(h, macro, values, comment)
            if nargin==4
                h.printComment(comment);
            end
            h.printLine([macro, ' := ', h.concatenate(values)]);
        end
        
        % -----------------------------------------------------------------
        % Print a concatenation
        % -----------------------------------------------------------------
        function printConcatenation(h, macro, values, comment)
            if nargin==4
                h.printComment(comment);
            end
            h.printLine([macro, ' += ', h.concatenate(values)]);
        end
        
    end % methods
    
    methods (Static)
        function value = transformPaths(value, platform)            
            value = transformPaths@coder.make.MakefileWriter(value, platform);
            
            % Any paths that have a '#' in them have to be escaped with a
            % backslash
            value = regexprep(value, '#', '\\#');           
        end
    end
    
end

% LocalWords:  gmake addsuffix notdir addprefix
