gusucode.com > private工具箱matlab源码程序 > private/code_unix_make_file.m

    function code_unix_make_file(fileNameInfo,buildInfo,modelName)
% CODE_UNIX_MAKE_FILE(FILENAMEINFO)

%   Copyright 1995-2015 The MathWorks, Inc.
%     
    
    global gMachineInfo gTargetInfo
    ext = '.o';
    code_machine_objlist_file(fileNameInfo, buildInfo, ext);

    fileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.unixMakeFile);
   sf_echo_generating('Coder',fileName);
    file = fopen(fileName,'Wt');
    if file<3
        construct_coder_error([],sprintf('Failed to create file: %s.',fileName),1);
    end

    DOLLAR = '$';
fprintf(file,'#--------------------------- Tool Specifications -------------------------\n');
fprintf(file,'#\n');
fprintf(file,'# Modify the following macros to reflect the tools you wish to use for\n');
fprintf(file,'# compiling and linking your code.\n');
fprintf(file,'#\n');
    if(~isempty(fileNameInfo.userMakefiles))
        for i=1:length(fileNameInfo.userMakefiles)
fprintf(file,'include $fileNameInfo.userMakefiles{i}\n');
        end
    end
    if(gTargetInfo.codingMakeDebug)
fprintf(file,'CC = %s/bin/mex -g\n',matlabroot);
    else
fprintf(file,'CC = %s/bin/mex\n',matlabroot);
    end
fprintf(file,'LD = %s(CC)\n',DOLLAR);
fprintf(file,' \n');
fprintf(file,'MODEL     = %s\n',gMachineInfo.machineName);
fprintf(file,'TARGET      = %s\n',gMachineInfo.targetName);

    moduleSrcs = getCatString(buildInfo.getSourceFiles(false,true,'MODULE_SRCS'));
fprintf(file,'MODULE_SRCS   = %s\n',moduleSrcs);

    modelSrc = getCatString(buildInfo.getSourceFiles(false,true,'MODEL_SRC'));
fprintf(file,'MODEL_SRC  = %s\n',modelSrc);
    modelReg = getCatString(buildInfo.getSourceFiles(false,true,'MODEL_REG'));
fprintf(file,'MODEL_REG = %s\n',modelReg);

fprintf(file,'MAKEFILE    = %s\n',fileNameInfo.unixMakeFile);

fprintf(file,'MATLAB_ROOT  = %s\n',fullfile(sf('Root'),'..','..','..'));
fprintf(file,'BUILDARGS   = \n');
    

fprintf(file,'#------------------------------ Include/Lib Path ------------------------------\n');
fprintf(file,' \n');
userIncludeDirString = '';
userIncludeDirs = buildInfo.getIncludePaths(true,'USER_INCLUDES');
if ~isempty(userIncludeDirs)
    for i = 1:length(userIncludeDirs)
        path = escapePathStr(userIncludeDirs{i});
        userIncludeDirString = [userIncludeDirString,'-I"',path,'" ']; %#ok<AGROW>
    end
end
fprintf(file,'USER_INCLUDES = %s\n',userIncludeDirString);
    
auxIncludeDirString = '';
auxincludePaths = buildInfo.getIncludePaths(true,'AUX_INCLUDES');
if ~isempty(auxincludePaths)
    for i = 1:length(auxincludePaths)
        path = auxincludePaths{i};
        auxIncludeDirString = [auxIncludeDirString,'-I"',path,'" ']; %#ok<AGROW>
    end
end

fprintf(file,'AUX_INCLUDES = %s\n',auxIncludeDirString);
% All file Inlcudes
mlslsfInclude = buildInfo.getIncludePaths(true,{'ML_INCLUDES','SL_INCLUDES','SF_INCLUDES'});
fprintf(file,'MLSLSF_INCLUDES  = \\\n');
    for i = 1:numel(mlslsfInclude) -1
fprintf(file,'    -I"%s" \\\n',mlslsfInclude{i});
    end
fprintf(file,'    -I"%s" \n',mlslsfInclude{end});
fprintf(file,'\n');
thirdPartyIncludes = buildInfo.getIncludePaths(true,{},{'USER_INCLUDES','AUX_INCLUDES',...
    'ML_INCLUDES','SL_INCLUDES','SF_INCLUDES'});
thirdPartyIncludesString = '';
for i = 1:length(thirdPartyIncludes)
    path = thirdPartyIncludes{i};
    thirdPartyIncludesString = [thirdPartyIncludesString,'-I"',path,'" ']; %#ok<AGROW>
end
fprintf(file,'THIRD_PARTY_INCLUDES = %s\n',thirdPartyIncludesString);
fprintf(file,'\n');
fprintf(file,'INCLUDE_PATH = %s(USER_INCLUDES) %s(AUX_INCLUDES) %s(MLSLSF_INCLUDES) %s(COMPILER_INCLUDES) %s(THIRD_PARTY_INCLUDES)\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,' \n');

fprintf(file,'#----------------- Compiler and Linker Options --------------------------------\n');
fprintf(file,' \n');
fprintf(file,'# Optimization Options\n');

fprintf(file,' \n');
fprintf(file,'CC_OPTS = %s\n',getCatString(buildInfo.getCompileFlags));
fprintf(file,'CPP_REQ_DEFINES = -DMATLAB_MEX_FILE\n');
fprintf(file,' \n');
fprintf(file,'# Uncomment this line to move warning level to W4\n');
fprintf(file,'# cflags = %s(cflags:W3=W4)\n',DOLLAR);
fprintf(file,'CFLAGS = %s(CC_OPTS) %s(CPP_REQ_DEFINES) %s(INCLUDE_PATH)\n',DOLLAR,DOLLAR,DOLLAR);
fprintf(file,' \n');
fprintf(file,'LDFLAGS = %s\n',getCatString(buildInfo.getLinkFlags));
fprintf(file,' \n');
fprintf(file,'AUXLDFLAGS = %s\n',getCatString(buildInfo.getLinkFlags('AUXLDFLAGS')));

fprintf(file,'#----------------------------- Source Files -----------------------------------\n');
fprintf(file,' \n');
fprintf(file,'REQ_SRCS  = %s(MODEL_SRC) %s(MODEL_REG) %s(MODULE_SRCS)\n',DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'\n');
userAbsSources = buildInfo.getSourceFiles(true,true,'USER_ABS_SRCS');
userSources = buildInfo.getSourceFiles(true,true,'USER_SRCS');

    if(~isempty(userAbsSources))
fprintf(file,'USER_ABS_OBJS    = \\\n');
        for i=1:length(userAbsSources)
            [~, nameStr] = fileparts(userAbsSources{i});
            objStr = [nameStr '.o'];
fprintf(file,'		%s \\\n',objStr);
        end
    else
fprintf(file,'USER_ABS_OBJS =\n');
    end
fprintf(file,'\n');
auxSources = buildInfo.getSourceFiles(true,true,'AUX_SRCS');   
if ~isempty(auxSources)
fprintf(file,'AUX_ABS_OBJS = \\\n');
    for i=1:numel(auxSources)
        [~, nameStr] = fileparts(auxSources{i});
        objStr = [nameStr '.o'];
fprintf(file,'		%s \\\n',objStr);
    end
fprintf(file,'\n');
else
fprintf(file,'AUX_ABS_OBJS =\n');
end

thirdPartyExcludeGroups = {'MODULE_SRCS','MODEL_SRC','MODEL_REG','USER_SRCS','USER_ABS_SRCS','AUX_SRCS'};
thirdPartySources = buildInfo.getSourceFiles(false,true,{}, thirdPartyExcludeGroups);
% Third Party Source Paths contain only paths specfied in
% coder.ExternalDepedency or coder.Buildable
thirdPartySourcePaths = buildInfo.getSourcePaths(true);
    if(~isempty(thirdPartySources))
fprintf(file,'THIRD_PARTY_OBJS     = \\\n');
        for i=1:length(thirdPartySources)
            [~, nameStr] = fileparts(thirdPartySources{i});
            objStr = [nameStr '.o'];
fprintf(file,'		%s \\\n',objStr);
        end
    else
fprintf(file,'THIRD_PARTY_OBJS =\n');
    end
fprintf(file,'\n');
    
fprintf(file,'REQ_OBJS = %s(REQ_SRCS:.cpp=.o)\n',DOLLAR);
fprintf(file,'REQ_OBJS2 = %s(REQ_OBJS:.c=.o)\n',DOLLAR);
fprintf(file,'OBJS = %s(REQ_OBJS2) %s(USER_ABS_OBJS) %s(AUX_ABS_OBJS) %s(THIRD_PARTY_OBJS)\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'OBJLIST_FILE = %s\n',fileNameInfo.objListFile);

arch = lower(computer);

fprintf(file,'SFCLIB = %s\n',getLinkObjStringForGroup(buildInfo,'SFCLIB'));

fprintf(file,'AUX_LNK_OBJS = %s\n',getLinkObjStringForGroup(buildInfo,'AUX_LNK_OBJS'));
fprintf(file,'USER_LIBS = %s\n',getLinkObjStringForGroup(buildInfo,'USER_LIBS'));
    
    
fprintf(file,'PARLIB = %s\n',getLinkObjStringForGroup(buildInfo,'PARLIB'));

fprintf(file,'  MAPCSF = %s/tools/%s/mapcsf\n',matlabroot,arch);
fprintf(file,'   # RUN_MAPCSF_ON_UNIX is defined only if MAPCSF exists on this platform.\n');
fprintf(file,'   ifneq (%s(wildcard %s(MAPCSF)),) # run MAPCSF if it exists on this platform\n',DOLLAR,DOLLAR);
fprintf(file,'      RUN_MAPCSF_ON_UNIX =  %s/tools/%s/mapcsf %s@\n',matlabroot,arch,DOLLAR);
fprintf(file,'   endif\n');


excludeGroups = {'SFCLIB','USER_LIBS','LINK_MACHINE_LIBS', ...
     'LINK_MACHINE_OBJLIST','TMWLIB','PARLIB'};

userGroups = {};
if gTargetInfo.codingLibrary
    libObjListfile = ['lib_' fileNameInfo.objListFile];
else
    libObjListfile = fileNameInfo.objListFile;
end
code_append_libs_to_objlist_file_unix(libObjListfile, ...
    fileNameInfo.targetDirName, buildInfo,userGroups,excludeGroups);
CGXE.Coder.code_append_syslibs_to_objlist_file(libObjListfile,fileNameInfo.targetDirName, ...
    buildInfo, modelName);  %Third Party Libraries
fprintf(file,' \n');
fprintf(file,'#--------------------------------- Rules --------------------------------------\n');
fprintf(file,' \n');
    if gTargetInfo.codingLibrary

if ismac
fprintf(file,'DO_RANLIB = xcrun ranlib %s(MODEL)_%s(TARGET).a\n',DOLLAR,DOLLAR);
else
fprintf(file,'DO_RANLIB = ranlib %s(MODEL)_%s(TARGET).a\n',DOLLAR,DOLLAR);
end
fprintf(file,' \n');
fprintf(file,'%s(MODEL)_%s(TARGET).a : %s(MAKEFILE) %s(OBJS) %s(SFCLIB) %s(AUX_LNK_OBJS) %s(USER_LIBS) %s(THIRD_PARTY_LIBS)\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'	@echo ### Linking ...\n');
if ismac
fprintf(file,'	xcrun ar ruv %s(MODEL)_%s(TARGET).a %s(OBJS)\n',DOLLAR,DOLLAR,DOLLAR);
else
fprintf(file,'	ar ruv %s(MODEL)_%s(TARGET).a %s(OBJS)\n',DOLLAR,DOLLAR,DOLLAR);
end
fprintf(file,'	%s(DO_RANLIB)\n',DOLLAR);
    else
        % Due to g1205457 added the list of libs to the objlist file only for the MEX file
        % we do not want to archive libraries into intermediate static libraries
        code_append_libs_to_objlist_file_unix(libObjListfile, ...
            fileNameInfo.targetDirName, buildInfo, 'LINK_MACHINE_LIBS');
        code_append_libs_to_objlist_file_unix(libObjListfile, ...
            fileNameInfo.targetDirName, buildInfo, 'TMWLIB');
        code_append_libs_to_objlist_file_unix(libObjListfile,...
            fileNameInfo.targetDirName, buildInfo, 'LINK_MACHINE_OBJLIST');
        if(gTargetInfo.codingMEX && ~isempty(sf('get',gMachineInfo.target,'target.mexFileName')))
fprintf(file,'MEX_FILE_NAME = %s.%s\n',sf('get',gMachineInfo.target,'target.mexFileName'),mexext);
        else
fprintf(file,'MEX_FILE_NAME = %s(MODEL)_%s(TARGET).%s\n',DOLLAR,DOLLAR,mexext);
        end
fprintf(file,' \n');
fprintf(file,' %s(MEX_FILE_NAME): %s(MAKEFILE) %s(OBJS) %s(SFCLIB) %s(AUX_LNK_OBJS) %s(USER_LIBS)\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'	@echo ### Linking ...\n');
fprintf(file,'	%s(CC) -silent LDFLAGS="\\%s%sLDFLAGS %s(LDFLAGS) %s(AUXLDFLAGS)" -output %s(MEX_FILE_NAME) @%s(OBJLIST_FILE) %s(AUX_LNK_OBJS) %s(USER_LIBS) %s(SFCLIB) %s(PARLIB) %s(IPPLIB) %s(THIRD_PARTY_LIBS)\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'	%s(RUN_MAPCSF_ON_UNIX)\n',DOLLAR);
fprintf(file,'\n');
    end
fprintf(file,'%%.o :    %%.c\n');
fprintf(file,'	%s(CC) -c %s(CFLAGS) %s<\n',DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'\n');
fprintf(file,'%%.o :    %%.cpp\n');
fprintf(file,'	%s(CC) -c %s(CFLAGS) %s<\n',DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'\n');
        for i=1:length(userSources)
            objFileName = code_unix_change_ext(userSources{i}, 'o');
fprintf(file,'%s :	%s\n',objFileName,escapePathStr(userSources{i}));
fprintf(file,'	%s(CC) -c %s(CFLAGS) %s\n',DOLLAR,DOLLAR,escapePathStr(userSources{i}));
        end

    for i=1:length(thirdPartySources)
        [pathStr, nameStr,ext] = fileparts(thirdPartySources{i});
        objFileName = code_unix_change_ext(thirdPartySources{i}, 'o');
        fullSrcName = thirdPartySources{i};
        if isempty(pathStr)
            fullSrcPath = sfprivate('tokenize',[],[nameStr ext],'custom source files string',...
                thirdPartySourcePaths);
            if ~isempty(fullSrcPath)
                fullSrcName = fullSrcPath{1};  
            end
        end
fprintf(file,'%s :	%s\n',objFileName,escapePathStr(fullSrcName));
fprintf(file,'	%s(CC) -c %s(CFLAGS) %s\n',DOLLAR,DOLLAR,escapePathStr(fullSrcName));
    end
    
    fclose(file);

function result = code_unix_change_ext(filename, ext)

[~, name_str] = fileparts(filename);

result = [name_str '.' ext];

function linkObjStr =  getLinkObjStringForGroup(buildInfo,group, excludeGroups)
%Helper function to create a string that contatenates the Link objects in
%the platform specific format for a given group.
if nargin < 3
    excludeGroups ={};
end
linkObjs = buildInfo.getLinkObjects(group,excludeGroups);
linkObjStr = '';
for iL=  1:numel(linkObjs)
    currLinkObjPath = escapePathStr(strrep(linkObjs(iL).Path,'$(MATLAB_ROOT)',matlabroot));
    if any(ismember(group,{'SFCLIB','USER_LIBS','LINK_MACHINE_LIBS', ...
            'AUX_LNK_OBJS'})) || isempty(group)
        %Third Party Library have empty group
        linkObjStr = [linkObjStr currLinkObjPath filesep linkObjs(iL).Name  ' ']; %#ok<AGROW>
    else
        linkObjStr = [linkObjStr '-L' currLinkObjPath ' -l' linkObjs(iL).Name ' ']; %#ok<AGROW>
        
    end
end
function str = getCatString(incell)
%Conatenate all the strings in cell array of strings.
str = sprintf('%s ',incell{:});
if ~isempty(str)
    str(end) = [];
end

function escStr = escapePathStr(inpath)
% Escape scapces in path so that the makefile will work with directories
% having spaces in their path
escStr = regexprep(strtrim(inpath),' ','\\ ');