classdef BuildInfo < RTW.BuildInfoBuildObj & handle & matlab.mixin.SetGet & matlab.mixin.Copyable
%RTW.BuildInfo class
%   RTW.BuildInfo extends RTW.BuildInfoBuildObj.
%
%    RTW.BuildInfo properties:
%       Settings - Property is of type 'handle'  
%       DisplayLabel - Property is of type 'ustring'  
%       Group - Property is of type 'ustring'  
%       Inc - Property is of type 'handle'  
%       Src - Property is of type 'handle'  
%       Other - Property is of type 'handle'  
%       ModelHandle - Property is of type 'double'  
%       ModelName - Property is of type 'ustring'  
%       Viewer - Property is of type 'handle'  
%       Options - Property is of type 'handle'  
%       Tokens - Property is of type 'handle vector'  
%       BuildArgs - Property is of type 'handle vector'  
%       MakeVars - Property is of type 'handle vector'  
%       MakeArgs - Property is of type 'ustring'  
%       TargetPreCompLibLoc - Property is of type 'ustring'  
%       TargetLibSuffix - Property is of type 'ustring'  
%       InstallDirmexopts - Property is of type 'ustring'  
%       LinkObj - Property is of type 'handle vector'  
%       ModelRefs - Property is of type 'handle vector'  
%       SysLib - Property is of type 'handle'  
%       ReportInfo - Property is of type 'mxArray'  
%       BuildTools - Property is of type 'mxArray'  
%       Maps - Property is of type 'mxArray'  

%   Copyright 2017-2020 The MathWorks, Inc.

    properties
        ComponentName char = '';
        Viewer = [];
        Tokens = [];
        BuildArgs = [];
        MakeVars = [];
        MakeArgs char = '';
        TargetPreCompLibLoc char = '';
        TargetLibSuffix char = '';
        InstallDirmexopts char = '';
        ModelRefs = [];
        SysLib = [];
        ReportInfo = [];
        Maps = [];
    end
    properties (Dependent = true, Hidden = true, Access=public)
        % ModelName is an alias for ComponentName
        % ComponentName should be preferred to ModelName
        ModelName char;
    end
    properties (Hidden = true)
        ModelHandle double = 0;
    end
    properties (SetAccess = private)
        LinkObj = [];
    end
    % Properties that must not be serialized
    properties(Hidden = true, Transient=true)
        BuildTools = [];
        ComponentBuildFolder char = ''
    end

    methods
        
        function set.ComponentBuildFolder(this, val)
        % The component build folder is always stored as a relative path. It can be
        % specified directly as a relative path or as a path beginning with the $(START_DIR)
        % root folder.
            
            if ~isempty(val) 
                lRootFolder = this.Settings.LocalAnchorDir;
                if isempty(lRootFolder)
                    lRootFolder = pwd;
                end
                
                % Make sure we have a canonical path
                if coder.make.internal.isRelativePath(val) 
                    val = fullfile(lRootFolder, val);
                end
                val = builtin('_canonicalizepath', val);
                
                assert(startsWith...
                       (val, lRootFolder), ...
                       'The component build folder must be a relative path or path starting with %s', ...
                       lRootFolder)
                
                % Obtain a relative path by stripping off the leading "<root folder>", 
                % for example, if root folder is "Z:\" then "Z:\build_folder" becomes
                % "build_folder" or if root folder is "Z:\myfolder" then "Z:\my_folder\build_folder"
                % becomes "\build_folder"
                val = val((length(lRootFolder)+1:end));
                
                % Remove any leading file separator
                val = regexprep(val, ['^' regexptranslate('escape', filesep)], '');
            end
            this.ComponentBuildFolder = val;
        end
            

        function h = BuildInfo(varargin)
            if (nargin >= 1)
                h.ModelHandle  = varargin{1};
            else
                h.ModelHandle  = 0;
            end
            
            if (h.ModelHandle ~= 0)
                h.ModelName = get_param(h.ModelHandle,'Name');
            else
                h.ModelName = 'untitled';
            end
            
            h.Settings     = RTW.BuildInfoSettings;
            h.Options      = RTW.BuildInfoOptions(h.Settings);
            h.Inc          = RTW.BuildInfoModules('Includes',h.Settings);
            h.Src          = RTW.BuildInfoModules('Sources',h.Settings);
            h.Other        = RTW.BuildInfoModules('Other',h.Settings);
            h.LinkObj      = [];
            h.ModelRefs    = [];
            h.SysLib       = RTW.BuildInfoModules('SysLibs',h.Settings);
            h.DisplayLabel = 'Build Info';
            
            h.Maps.LinkObjects = [];
            h.Maps.ModelRefs = [];
        end
        % ModelName is an alias for ComponentName
        % ComponentName should be preferred to ModelName
        function set.ModelName(obj,str)
            obj.ComponentName = str;
        end        
        function str = get.ModelName(obj)
            str = obj.ComponentName;
        end
    end

    methods
        addBuildArgs(h,varargin)
        addExternalModeInfo(buildInfo,extModeTransport,rtiostreamSrcFileFullPath)
        addKeyValuePair(h,Type,varargin)
        addLCCSocketsLib(buildInfo)
        ret = addLibraries(h,varargin)
        ret = addLinkObjects(h,varargin)
        addMakeVars(h,varargin)
        addSysLibPaths(h,varargin)
        ret = addSysLibs(h,varargin)
        addTMFTokens(h,varargin)
        val = areChildrenOrdered(h)
        checkForDups(~,fPathNames,names)
        ret = checkForUnresolvedFiles(h,varargin)
        deleteKeyValuePair(h,Type,varargin)
        deleteTMFTokens(h,varargin)
        [key,val] = findBuildArg(h,keyname)
        findIncludeFiles(h,varargin)
        kvPair = findKeyValuePair(h,Type,keyName)
        ret = findLinkObject(h,libname)
        [key,val] = findTMFToken(h,keyname)
        [Keys,Vals] = getBuildArgs(h,varargin)
        ret = getBuildDirList(h,varargin)
        buildName = getBuildName(h)
        val = getChildren(h)
        dlgstruct = getDialogSchema(hSrc,schemaName)
        [fPathNames,names] = getFullFileList(h,varargin)
        Groups = getGroups(h)
        val = getHierarchicalChildren(h)
        [Keys,Vals] = getKeyValuePair(h,varargin)
        ret = getLibraryPaths(h,varargin)
        lObjs = getLinkObjects(h,varargin)
        ret = getLocalBuildDir(h)
        [MakeStrs,Vars,Vals] = getMakeVars(h)
        [syslibs,libpaths] = getSysLibInfo(h)
        v = getViewer(h)
        val = isHierarchical(h)
        packNGo(buildInfo,varargin)
        printToFile(h,varargin)
        removeLinkObjects(h, linkObjNames, groups)
        updateFilePathsAndExtensions(h,varargin)
        updateFileSeparator(h,varargin)
    end


    methods (Hidden)
        setRootFolder(h, val);
        setOutputFolder(h,lOutputFolder)
        resp = getBuildToolInfo(h,field)
        [sDirFiles,mlrFiles,fPathNames,names,pathToTopBuildInfo] = getHierarchicalFileList(h,fPathNames,names)
        postLoadUpdate(h,buildInfoMatPath)       
        replacePathString(h,oldStr,newStr)        
        setStartDir(h,startDir)
        setTargetProvidesMain(h,targetProvidesMain)
    end
end

