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"), ¤tPath) )
{
...
return;
}
...
wxString masterPath = compiler->GetMasterPath();
...
wxPathList pathList;
if ( !masterPath.Trim().IsEmpty() )
{
pathList.Add(masterPath + pathSep + _T("bin"));
pathList.Add(masterPath);
}
...
...
...
...
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();
if ( m_pProject && m_pProject->GetCompilerID() != m_CompilerId)
SwitchCompiler(m_pProject->GetCompilerID());
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:
{
...
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)
{
BuildStateManagement();
cmd = m_CommandQueue.Next();
if (!cmd && m_BuildState == bsNone && m_NextBuildState == bsNone)
{
...
return 0;
}
if (cmd)
break;
}
}
...
return DoRunQueue();
}