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

    function code_msvc_make_file(fileNameInfo,buildInfo,modelName)
% CODE_MSVC_MAKE_FILE(FILENAMEINFO)

%   Copyright 1995-2015 The MathWorks, Inc.
%     

    global gMachineInfo gTargetInfo
    
    code_machine_objlist_file(fileNameInfo, buildInfo);

    fileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.makeBatchFile);
  sf_echo_generating('Coder',fileName);
    file = fopen(fileName,'Wt');
    if file<3
        construct_coder_error([],sprintf('Failed to create file: %s.',fileName),1);
    end
    create_mexopts_caller_bat_file(file,fileNameInfo);
fprintf(file,'nmake -f %s\n',fileNameInfo.msvcMakeFile);
    fclose(file);

    fileName = fullfile(fileNameInfo.targetDirName,fileNameInfo.msvcMakeFile);
  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,'# ------------------- Required for MSVC nmake ---------------------------------\n');
fprintf(file,'# This file should be included at the top of a MAKEFILE as follows:\n');
fprintf(file,'\n');
fprintf(file,'\n');
    if(~isempty(fileNameInfo.userMakefiles))
        for i=1:length(fileNameInfo.userMakefiles)
fprintf(file,'!include "%s"\n',fileNameInfo.userMakefiles{i});
        end
    end
        if(strcmp(computer,'PCWIN64'))
fprintf(file,'CPU = AMD64\n');
        end
    msCompilersWithWin32mak = { 'msvc100', 'msvcpp100', ...
                                'msvc90' , 'msvcpp90' , ...
                                'msvc80' , 'msvcpp80' , ...
                                'mssdk71', 'mssdk71cpp'};
    if ismember(gTargetInfo.compilerName, msCompilersWithWin32mak)
    % MSVC2012 and later no longer ships "Win32.Mak".
fprintf(file,'!include <ntwin32.mak>\n');
    end
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.msvcMakeFile);

fprintf(file,'MATLAB_ROOT  = %s\n',fileNameInfo.matlabRoot);
fprintf(file,'BUILDARGS   =\n');

fprintf(file,'\n');
fprintf(file,'#--------------------------- Tool Specifications ------------------------------\n');
fprintf(file,'#\n');
fprintf(file,'#\n');
fprintf(file,'MSVC_ROOT1 = %s(MSDEVDIR:SharedIDE=vc)\n',DOLLAR);
fprintf(file,'MSVC_ROOT2 = %s(MSVC_ROOT1:SHAREDIDE=vc)\n',DOLLAR);
fprintf(file,'MSVC_ROOT  = %s(MSVC_ROOT2:sharedide=vc)\n',DOLLAR);
fprintf(file,'\n');
fprintf(file,'# Compiler tool locations, CC, LD, LIBCMD:\n');
fprintf(file,'CC     = cl.exe\n');
fprintf(file,'LD     = link.exe\n');
fprintf(file,'LIBCMD = lib.exe\n');

fprintf(file,'#------------------------------ Include/Lib Path ------------------------------\n');
fprintf(file,'\n');
    userIncludeDirString = '';
    userIncludeDirs = buildInfo.getIncludePaths(true,'USER_INCLUDES');
    for i = 1:length(userIncludeDirs)
        thisIncDir = userIncludeDirs{i};
        if ~isempty(regexp(thisIncDir, '^[a-zA-Z]+:$', 'once'))
            % G602058: MSVC has a bug that a drive root represented by
            % "c:" is not recognized. The workaround is to use "c:\."
            thisIncDir = [thisIncDir '\.'];  %#ok<AGROW>
        end
        userIncludeDirString    = [userIncludeDirString,' /I "',thisIncDir,'"']; %#ok<AGROW>
    end
fprintf(file,'USER_INCLUDES   = %s\n',userIncludeDirString);
    
auxIncludeDirString = '';
auxincludePaths = buildInfo.getIncludePaths(true,'AUX_INCLUDES');
for i = 1:length(auxincludePaths)
    auxIncludeDirString = [auxIncludeDirString,' /I "',auxincludePaths{i},'"']; %#ok<AGROW>
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');
fprintf(file,'COMPILER_INCLUDES = /I "%s(MSVC_ROOT)\\include"\n',DOLLAR);
fprintf(file,'\n');

    thirdPartyIncludeString = '';
    thirdPartyincludePaths = buildInfo.getIncludePaths(true,{},{'USER_INCLUDES',...
        'AUX_INCLUDES','ML_INCLUDES','SL_INCLUDES','SF_INCLUDES'});
    for i = 1:length(thirdPartyincludePaths)
        thirdPartyIncludeString = [thirdPartyIncludeString,' /I "',thirdPartyincludePaths{i},'"']; %#ok<AGROW>
    end
fprintf(file,'THIRD_PARTY_INCLUDES   = %s\n',thirdPartyIncludeString);
    
fprintf(file,'INCLUDE_PATH = %s(USER_INCLUDES) %s(AUX_INCLUDES) %s(MLSLSF_INCLUDES)\\\n',DOLLAR,DOLLAR,DOLLAR);
fprintf(file,' %s(THIRD_PARTY_INCLUDES)\n',DOLLAR);

fprintf(file,'LIB_PATH     = "%s(MSVC_ROOT)\\lib"\n',DOLLAR);

fprintf(file,'\n');

fprintf(file,'CFLAGS = %s\n',getCatString(buildInfo.getCompileFlags));
fprintf(file,'LDFLAGS = %s\n',getCatString(buildInfo.getLinkFlags));



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 '.obj'];
fprintf(file,'     "%s" \\\n',objStr);
        end
    else
fprintf(file,'USER_ABS_OBJS =\n');
    end
fprintf(file,'\n');
% All other source files
auxSourceFiles  =  buildInfo.getSourceFiles(true,true,'AUX_SRCS');
if ~isempty(auxSourceFiles)
fprintf(file,'AUX_ABS_OBJS = \\\n');
    for i=1:numel(auxSourceFiles)
        [~, nameStr] = fileparts(auxSourceFiles{i});
        objStr = [nameStr '.obj'];
fprintf(file,'     "%s" \\\n',objStr);
    end
else
fprintf(file,'AUX_ABS_OBJS =\n');
end
fprintf(file,'\n');
    
thirdPartyExcludeGroups = {'MODULE_SRCS','MODEL_SRC','MODEL_REG', ...
    'AUX_SRCS','USER_ABS_SRCS','USER_SRCS'};
thirdPartySources  =  buildInfo.getSourceFiles(true,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 '.obj'];
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=.obj)\n',DOLLAR);
fprintf(file,'REQ_OBJS2 = %s(REQ_OBJS:.c=.obj)\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);

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'));
    
excludeGroups = {'SFCLIB', 'AUX_LNK_OBJS','USER_LIBS', ...
    'LINK_MACHINE_LIBS', 'LINK_MACHINE_OBJLIST', 'TMWLIB'};
userGroups ={};
if gTargetInfo.codingLibrary
    libObjListfile = ['lib_' fileNameInfo.objListFile];
else
    libObjListfile = fileNameInfo.objListFile;
end

code_append_libs_to_objlist_file_msvc(libObjListfile, ...
    fileNameInfo.targetDirName, buildInfo,userGroups,excludeGroups);
CGXE.Coder.code_append_syslibs_to_objlist_file(libObjListfile,fileNameInfo.targetDirName, ...
    buildInfo, modelName);  %Third Party Libraries
fprintf(file,'#--------------------------------- Rules --------------------------------------\n');
fprintf(file,'\n');
    if gTargetInfo.codingLibrary  
       
fprintf(file,'%s(MODEL)_%s(TARGET).lib : %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');
fprintf(file,'	%s(LD) -lib /OUT:%s(MODEL)_%s(TARGET).lib @%s(OBJLIST_FILE) %s(USER_LIBS) %s(THIRD_PARTY_LIBS)\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'	@echo ### Created Stateflow library %s@\n',DOLLAR);

    else 
        % Due to g1205457 added the list of libs to the objlist file only for
        % the MEX file's Makefile
        code_append_libs_to_objlist_file_msvc(libObjListfile,...
            fileNameInfo.targetDirName, buildInfo, 'LINK_MACHINE_LIBS');
        code_append_libs_to_objlist_file_msvc(libObjListfile,...
            fileNameInfo.targetDirName, buildInfo, 'LINK_MACHINE_OBJLIST');
        code_append_libs_to_objlist_file_msvc(libObjListfile, ...
            fileNameInfo.targetDirName, buildInfo, 'TMWLIB');

        if(gTargetInfo.codingMEX && ~isempty(sf('get',gMachineInfo.target,'target.mexFileName')))

fprintf(file,'MEX_FILE_NAME_WO_EXT = %s\n',sf('get',gMachineInfo.target,'target.mexFileName'));
        else
fprintf(file,'MEX_FILE_NAME_WO_EXT = %s(MODEL)_%s(TARGET)\n',DOLLAR,DOLLAR);
        end
fprintf(file,'MEX_FILE_NAME = %s(MEX_FILE_NAME_WO_EXT).%s\n',DOLLAR,mexext);
        mapCsfBinary = '';

        if(~isempty(mapCsfBinary))
fprintf(file,'MEX_FILE_CSF =  %s(MEX_FILE_NAME_WO_EXT).csf\n',DOLLAR);
        else
fprintf(file,'MEX_FILE_CSF =\n');
        end

fprintf(file,'all : %s(MEX_FILE_NAME) %s(MEX_FILE_CSF)\n',DOLLAR,DOLLAR);
fprintf(file,'\n');
        
fprintf(file,'%s(MEX_FILE_NAME) : %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);
fprintf(file,' @echo ### Linking ...\n');
fprintf(file,' %s(LD) %s(LDFLAGS) /OUT:%s(MEX_FILE_NAME) /map:"%s(MEX_FILE_NAME_WO_EXT).map"\\\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'  %s(USER_LIBS) %s(SFCLIB) %s(AUX_LNK_OBJS)\\\n',DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'  %s(DSP_LIBS) %s(THIRD_PARTY_LIBS)\\\n',DOLLAR,DOLLAR);
fprintf(file,'  @%s(OBJLIST_FILE)\n',DOLLAR);

    switch gTargetInfo.compilerName
        case cgxeprivate('supportedPCCompilers','microsoft')
fprintf(file,'     mt -outputresource:"%s(MEX_FILE_NAME);2" -manifest "%s(MEX_FILE_NAME).manifest"\n',DOLLAR,DOLLAR);
    end
fprintf(file,'	@echo ### Created %s@\n',DOLLAR);
fprintf(file,'\n');
        if(~isempty(mapCsfBinary))
fprintf(file,'%s(MEX_FILE_CSF) : %s(MEX_FILE_NAME)\n',DOLLAR,DOLLAR);
fprintf(file,'	"%s" %s(MEX_FILE_NAME)\n',mapCsfBinary,DOLLAR);
        end
    end

fprintf(file,'.c.obj :\n');
fprintf(file,'	@echo ### Compiling "%s<"\n',DOLLAR);
fprintf(file,'	%s(CC) %s(CFLAGS) %s(INCLUDE_PATH) "%s<"\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'\n');
fprintf(file,'.cpp.obj :\n');
fprintf(file,'	@echo ### Compiling "%s<"\n',DOLLAR);
fprintf(file,'	%s(CC) %s(CFLAGS) %s(INCLUDE_PATH) "%s<"\n',DOLLAR,DOLLAR,DOLLAR,DOLLAR);
fprintf(file,'\n');
        for i=1:length(userAbsSources)
            [~, nameStr] = fileparts(userAbsSources{i});
            objFileName = [nameStr '.obj'];
fprintf(file,'%s :  "%s"\n',objFileName,userSources{i});
fprintf(file,'	@echo ### Compiling "%s"\n',userSources{i});
fprintf(file,'	%s(CC) %s(CFLAGS) %s(INCLUDE_PATH) "%s"\n',DOLLAR,DOLLAR,DOLLAR,userSources{i});
        end
fprintf(file,'\n');
            for i=1:length(thirdPartySources)
            [pathStr, nameStr,ext] = fileparts(thirdPartySources{i});
            objFileName = [nameStr '.obj'];
            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,fullSrcName);
fprintf(file,'	@echo ### Compiling "%s"\n',fullSrcName);
fprintf(file,'	%s(CC) %s(CFLAGS) %s(INCLUDE_PATH) "%s"\n',DOLLAR,DOLLAR,DOLLAR,fullSrcName);
            end

    fclose(file);

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 = linkObjs(iL).Path;
    currLinkObjPath = strrep(currLinkObjPath,'$(MATLAB_ROOT)',matlabroot);
    linkObjStr = [linkObjStr '"' fullfile(currLinkObjPath,linkObjs(iL).Name) '" ']; %#ok<AGROW>
end
    
function str = getCatString(incell)
%Conatenate all the strings in cell array of strings.
str = sprintf('%s ',incell{:});
if ~isempty(str)
    str(end) = [];
end