⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 makefilegenerator.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 5 页
字号:

#ifdef __WXMSW__
void MakefileGenerator::DoAddMakefileResources(wxString& buffer)
{
    buffer << _T("### Resources used in this Makefile") << _T('\n');

    int targetsCount = m_Project->GetBuildTargetsCount();
    for (int x = 0; x < targetsCount; ++x)
    {
        ProjectBuildTarget* target = m_Project->GetBuildTarget(x);
        if (!target)
            break;

		// create target's options only if it has at least one linkable file
		if (!IsTargetValid(target))
			continue;

        buffer << target->GetTitle() << _T("_RESOURCE=");

        if (target->GetTargetType() == ttConsoleOnly)
        {
            buffer << _T('\n');
            break;
        }

        wxFileName resFile;
        resFile.SetName(target->GetTitle() + _T("_private"));
        resFile.SetExt(RESOURCEBIN_EXT);
        resFile.MakeRelativeTo(m_Project->GetBasePath());

        // now create the resource file...
        bool hasResources = false;
        wxString resBuf;
        resBuf << _T("#include <windows.h>") << _T('\n');
        int filesCount = (int)m_Files.GetCount();
        for (int i = 0; i < filesCount; ++i)
        {
            wxFileName file;

            ProjectFile* pf = m_Files[i];
            // if the file is allowed to compile *and* belongs in this target
            if (pf->link && pf->buildTargets.Index(target->GetTitle()) >= 0)
            {
                file.Assign(pf->relativeFilename);
                if (file.GetExt().Lower().Matches(_T("rc")))
                {
                    resBuf << _T("#include \"") << file.GetFullPath() << _T("\"") << _T('\n');
                    hasResources = true;
                }
            }
        }

        if (hasResources)
        {
            wxString out = UnixFilename(resFile.GetFullPath());
            ConvertToMakefileFriendly(out);
            QuoteStringIfNeeded(out);
            buffer << out << _T('\n');
            // write private resource file to disk
            resFile.Normalize(wxPATH_NORM_ALL & ~wxPATH_NORM_CASE, m_Project->GetBasePath());
            resFile.SetExt(RESOURCE_EXT);
            wxFile file(resFile.GetFullPath(), wxFile::write);
            cbWrite(file,resBuf);
        }
        else
            buffer << _T('\n');
    }
    buffer << _T('\n');
}
#endif // __WXMSW__

void MakefileGenerator::DoAddMakefileCreateDirs(wxString& buffer, ProjectBuildTarget* target, bool obj, bool dep, bool bin)
{
    if (!target)
        return;

    // create target's options only if it has at least one linkable file
    if (!IsTargetValid(target))
        return;

    wxArrayString addedDirs; // avoid creating multiple commands for the same dir
    int filesCount = (int)m_Files.GetCount();

    if (obj)
    {
        // object output dirs
        addedDirs.Clear();
        for (int i = 0; i < filesCount; ++i)
        {
            ProjectFile* pf = m_Files[i];
            // if the file belongs in this target
            if (pf->buildTargets.Index(target->GetTitle()) >= 0)
            {
                wxString sep = wxFileName::GetPathSeparator();
                wxString o_out = target->GetObjectOutput();
                wxString object_file = (!o_out.IsEmpty() ? o_out : _T(".")) +
                                       sep +
                                       pf->GetObjName();
                wxFileName o_file(object_file);
                wxFileName o_dir(o_file.GetPath(wxPATH_GET_SEPARATOR));
                RecursiveCreateDir(buffer, o_dir.GetDirs(), addedDirs);
            }
        }
    }

    if (dep)
    {
        // deps output dirs
        addedDirs.Clear();
        for (int i = 0; i < filesCount; ++i)
        {
            ProjectFile* pf = m_Files[i];
            // if the file belongs in this target
            if (pf->buildTargets.Index(target->GetTitle()) >= 0)
            {
                wxString sep = wxFileName::GetPathSeparator();
                wxString o_out = target->GetDepsOutput();
                wxString object_file = (!o_out.IsEmpty() ? o_out : _T(".")) +
                                       sep +
                                       pf->GetObjName();
                wxFileName o_file(object_file);
                wxFileName o_dir(o_file.GetPath(wxPATH_GET_SEPARATOR));
                RecursiveCreateDir(buffer, o_dir.GetDirs(), addedDirs);
            }
        }
    }

    if (bin)
    {
        // add output dir also
        addedDirs.Clear();
        wxFileName fname(target->GetOutputFilename());
        if (fname.IsAbsolute())
            fname.MakeRelativeTo(m_Project->GetBasePath());
        wxString out = UnixFilename(fname.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR));
        if (!out.IsEmpty())
        {
            ConvertToMakefileFriendly(out);
            QuoteStringIfNeeded(out);
            wxFileName o_file(out);
            wxFileName o_dir(o_file.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR));
            RecursiveCreateDir(buffer, o_dir.GetDirs(), addedDirs);
        }
    }
}

void MakefileGenerator::RecursiveCreateDir(wxString& buffer, const wxArrayString& subdirs, wxArrayString& guardList)
{
    wxString currdir;
    for (size_t i = 0; i < subdirs.GetCount(); ++i)
    {
    	wxString sub = subdirs[i];
#ifdef __WXMSW__
    	if (m_GeneratingMakefile)
    	{
            // Can't do it differently here...
            // We *must* replace the env vars if we 're running under windows
            // because the windows command shell is *really* dumb...
            // If we use an env. var in output and this env. var contains
            // path separators, it breaks under windows...
            Manager::Get()->GetMacrosManager()->ReplaceEnvVars(sub);
    	}
#endif

        currdir << sub;
        if (guardList.Index(currdir) != wxNOT_FOUND)
        {
            currdir << wxFileName::GetPathSeparator();
            continue;
        }
        guardList.Add(currdir);
#ifdef __WXMSW__
        buffer << _T("\t-@if not exist \"") << currdir << wxFileName::GetPathSeparator() << _T(".\" mkdir \"") << currdir << _T("\"\n");
#else
        wxString out = currdir;
        ConvertToMakefileFriendly(out);
        QuoteStringIfNeeded(out);
        buffer << _T("\t-@if ! test -d ") << out << _T("; then mkdir ") << out << _T("; fi\n");
#endif
        currdir << wxFileName::GetPathSeparator();
    }
}

void MakefileGenerator::DoAddMakefileObjs(wxString& buffer)
{
    buffer << _T("### Objects used in this Makefile") << _T('\n');

    int targetsCount = m_Project->GetBuildTargetsCount();
    for (int x = 0; x < targetsCount; ++x)
    {
        ProjectBuildTarget* target = m_Project->GetBuildTarget(x);
        if (!target)
            break;

		// create target's options only if it has at least one linkable file
		if (!IsTargetValid(target))
			continue;
        UpdateCompiler(target);

        wxString deps;
        wxString tmp;
		wxString tmpLink;
        int filesCount = (int)m_Files.GetCount();
        for (int i = 0; i < filesCount; ++i)
        {
            wxFileName file;

            ProjectFile* pf = m_Files[i];
            // if the file belongs in this target
            if (pf->buildTargets.Index(target->GetTitle()) >= 0)
            {
                if (FileTypeOf(pf->relativeFilename) == ftResource)
                    continue; // resource file are treated differently

				wxString fname = UnixFilename(pf->GetObjName());
//				ConvertToMakefileFriendly(fname);

				wxFileName deps_tmp = fname;
				deps_tmp.SetExt(_T("d"));
				wxString depsS;
                depsS << target->GetDepsOutput() << _T("/") << deps_tmp.GetFullPath();

				wxFileName objs_tmp = fname;
				wxString objsS;
                objsS << target->GetObjectOutput() << _T("/") << fname;

                objsS = UnixFilename(objsS);
                ConvertToMakefileFriendly(objsS);
                QuoteStringIfNeeded(objsS);
                depsS = UnixFilename(depsS);
                ConvertToMakefileFriendly(depsS);
                QuoteStringIfNeeded(depsS);

				if (pf->compile)
				{
                    deps << depsS << _T(" ");
                    tmp << objsS << _T(" "); // if the file is allowed to compile
                }
				if (pf->link)
					tmpLink << objsS << _T(" "); // if the file is allowed to link
            }
        }
        buffer << target->GetTitle() << _T("_OBJS=") << tmp << _T('\n');
        buffer << target->GetTitle() << _T("_LINKOBJS=");
		if (tmp.Matches(tmpLink))
			buffer << _T("$(") << target->GetTitle() << _T("_OBJS)");
		else
			buffer << tmpLink; // only write *_LINKOBJS if different from *_OBJS
//		if (target->GetTargetType() != ttConsoleOnly)
//			buffer << _T(" $(") << target->GetTitle() << _T("_RESOURCE)";
        buffer << _T('\n');
        if (m_CompilerSet->GetSwitches().needDependencies)
        {
            buffer << target->GetTitle() << _T("_DEPS=") << deps << _T('\n');
//            buffer << target->GetTitle() << _T("_DEPS=$(") << target->GetTitle() << _T("_OBJS:.";
//            buffer << m_CompilerSet->GetSwitches().objectExtension;
//            buffer << _T("=.d)") << _T('\n');
        }
    }
    buffer << _T('\n');
}

void MakefileGenerator::DoAddMakefileOptions(wxString& buffer)
{
    buffer << _T("### Compiler/linker options") << _T('\n');
    for (int i = 0; i < m_Project->GetBuildTargetsCount(); ++i)
    {
        ProjectBuildTarget* target = m_Project->GetBuildTarget(i);
        UpdateCompiler(target);
        if (!m_CompilerSet)
            continue;

        buffer << target->GetTitle() + _T("_GLOBAL_CFLAGS=");
        DoAppendCompilerOptions(buffer, 0L, true);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_PROJECT_CFLAGS=");
        DoAppendCompilerOptions(buffer, 0L);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_GLOBAL_LDFLAGS=");
        DoAppendLinkerOptions(buffer, 0L, true);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_PROJECT_LDFLAGS=");
        DoAppendLinkerOptions(buffer, 0L);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_GLOBAL_INCS=");
        DoAppendIncludeDirs(buffer, 0L, m_CompilerSet->GetSwitches().includeDirs, true);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_PROJECT_INCS=");
        DoAppendIncludeDirs(buffer, 0L, m_CompilerSet->GetSwitches().includeDirs);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_GLOBAL_LIBDIRS=");
        DoAppendLibDirs(buffer, 0L, m_CompilerSet->GetSwitches().libDirs, true);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_PROJECT_LIBDIRS=");
        DoAppendLibDirs(buffer, 0L, m_CompilerSet->GetSwitches().libDirs);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_GLOBAL_LIBS=");
        DoAppendLinkerLibs(buffer, 0L, true);
        buffer << _T('\n');

        buffer << target->GetTitle() + _T("_PROJECT_LIBS=");
        DoAppendLinkerLibs(buffer, 0L);
        buffer << _T('\n');
    }
	buffer << _T('\n');
}

void MakefileGenerator::DoAddMakefileIncludes(wxString& buffer)
{
    buffer << _T("### Targets include directories") << _T('\n');


    int targetsCount = m_Project->GetBuildTargetsCount();
    for (int x = 0; x < targetsCount; ++x)
    {
        ProjectBuildTarget* target = m_Project->GetBuildTarget(x);
        if (!target)
            break;

		// create target's options only if it has at least one linkable file
		if (!IsTargetValid(target))
			continue;

        wxString tmp;
        DoGetMakefileIncludes(tmp, target);

        buffer << target->GetTitle() << _T("_INCS=") << tmp << _T('\n');
    }
    buffer << _T('\n');
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -