パソコンでプログラミングしよう ウィンドウズC++プログラミング環境の構築
1.6.3.6(15)
ソースコード(Code::Blocksのコンパイラディレクトリ追加)

Code::BlocksのPATH改変におけるコンパイラディレクトリ追加をソースコードから解析する。

ダウンロードリンク

ソースコード(抜粋)

コンパイル環境のセットアップ

コンパイラディレクトリは環境セットアップの一部としてCompilerGCC::SetupEnvironment(plugins\compilergcc\compilergcc.cpp:682)がPATHへ追加する。

void CompilerGCC::SetupEnvironment()
{
...
Compiler* compiler = CompilerFactory::GetCompiler(m_CompilerId);
if (!compiler)
return;
wxString currentPath;
if ( !wxGetEnv(_T("PATH"), &currentPath) )
{
...
return;
}
...
// Get configured masterpath, expand macros and remove trailing separators
wxString masterPath = compiler->GetMasterPath();
...
// Compile new PATH list...
wxPathList pathList;
// [1] Pre-pend "master path" and "master path\bin"...
if ( !masterPath.Trim().IsEmpty() ) // Would be very bad, if it *is* empty
{
pathList.Add(masterPath + pathSep + _T("bin"));
pathList.Add(masterPath); // in case there is no "bin" sub-folder
}
// [2] Get configured extrapath(s), expand macros and remove trailing separators
...
// [3] Append what has already been in the PATH envvar...
// If we do it this way, paths are automatically normalized and doubles are removed
...
// Try to locate the path to the C compiler:
...
// Convert the pathList into a string to apply.
...
if ( !wxSetEnv(_T("PATH"), envPath) )
{
...
}
}

CompilerGCC::SetupEnvironmentはCompilerGCC::SwitchCompiler(plugins\compilergcc\compilergcc.cpp:954)がm_CompilerIdにコンパイラIDを代入した後にコールされ、コンパイラIDに従い環境をセットアップする。

プロジェクトチェック時のコンパイル環境セットアップ

CompilerGCC::SwitchCompilerをコールする主要な関数は二つある。一つはCompilerGCC::CheckProject(plugins\compilergcc\compilergcc.cpp:1000)で、[Build]メニューコマンドのほとんどが実行の最初にこれをコールしてプロジェクトの妥当性を確認する。

bool CompilerGCC::CheckProject()
{
AskForActiveProject();
// switch compiler for the project (if needed)
if ( m_pProject && m_pProject->GetCompilerID() != m_CompilerId)
SwitchCompiler(m_pProject->GetCompilerID());
// switch compiler for single file (if needed)
else if (!m_pProject && m_CompilerId != CompilerFactory::GetDefaultCompilerID())
SwitchCompiler(CompilerFactory::GetDefaultCompilerID());
return (m_pProject != 0L);
}

ターゲットビルド時のコンパイル環境セットアップ

CompilerGCC::SwitchCompilerをコールするもう一つの関数はCompilerGCC::BuildStateManagement(plugins\compilergcc\compilergcc.cpp:2308)で、ターゲットビルドのコンパイラをCompilerGCC::SwitchCompilerで選択し実行コマンドをキューに追加する。

void CompilerGCC::BuildStateManagement()
{
...
ProjectBuildTarget* bt = m_pBuildingProject->GetBuildTarget(GetTargetIndexFromName(m_pBuildingProject, m_BuildingTargetName));
...
if (...)
{
...
if (bt)
SwitchCompiler(bt->GetCompilerID());
...
}
...
wxArrayString cmds;
switch (m_NextBuildState)
{
...
case bsTargetBuild:
{
...
// run target build
if ( UseMake(m_pBuildingProject) )
{
...
}
else
cmds = dc.GetCompileCommands(bt);
...
break;
}
...
}
...
AddToCommandQueue(cmds);
...
}

CompilerGCC::BuildStateManagementは、[Build]メニューコマンドの実行部であるCompilerGCC::DoRunQueue(plugins\compilergcc\compilergcc.cpp:1203)がターゲットビルド要求を確認するときにコールする。なおCompilerGCC::DoRunQueueは複雑な再帰構造を持ちソースコード抜粋では十分な説明ができない。完全な理解は全ソースコードを参照する必要がある。

int CompilerGCC::DoRunQueue()
{
...
CompilerCommand* cmd = m_CommandQueue.Next();
if (!cmd)
{
...
while (1)
{
// keep switching build states until we have commands to run or reach end of states
BuildStateManagement();
cmd = m_CommandQueue.Next();
if (!cmd && m_BuildState == bsNone && m_NextBuildState == bsNone)
{
...
return 0;
}
if (cmd)
break;
}
}
...
return DoRunQueue();
}