Difference between revisions of "Some examples"

From Code::Blocks
(Create this page as collection of useful scripts)
 
(Add example script for loading files from a text file)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Scripting Code::Blocks]]
 +
__FORCETOC__
 +
 +
== Conditional compiler flags for different OS ==
 +
For example different libraries for X11 and Windows
 +
 +
1) Project->Build options->Select the project name on the left
 +
 +
2) Linker Settings->Other linker options
 +
 +
3) Add the squirrel script:
 +
 +
<code>
 +
<nowiki>
 +
[[if(PLATFORM == PLATFORM_MSW  ) print(_("-lgdi32")); else if(PLATFORM == PLATFORM_X11 ) printf(_("-lX11"));]]
 +
</nowiki>
 +
</code>
 +
 +
be aware, this has to be in one line of the other liner options...
  
 
== After build ==
 
== After build ==
Line 8: Line 27:
  
 
This is in one line, so you can copy and paste it in the post build step (but don't forget to add the <code>[[ ]]</code> )
 
This is in one line, so you can copy and paste it in the post build step (but don't forget to add the <code>[[ ]]</code> )
 +
 +
 +
== Add files from a text file to the active project ==
 +
<pre>
 +
<nowiki>
 +
local pm = GetProjectManager();
 +
if (IsNull(pm))
 +
{
 +
    ShowError(_T("Could not query the project manager. Cannot continue."));
 +
    return 0;
 +
}
 +
 +
local p = pm.GetActiveProject();
 +
 +
// File with the source files separated by \n
 +
// for example:
 +
// '''''''''''''''''''
 +
// a.cpp
 +
// b.cpp
 +
// '''''''''''''''''''
 +
local filecontent = IO.ReadFileContents (_T("D:\\ddddd\\fileList.txt"));
 +
 +
// Split the files in lines
 +
local fileList = GetArrayFromString(filecontent, _T("\n"), true);
 +
print(_T("Files to load:"));
 +
for(local i = 0; i < fileList.GetCount(); i++)
 +
{
 +
print(fileList.Item(i) + _T("\n"));  // Print the files in the list, as a check
 +
}
 +
 +
// Base path, the files in the text files are relative in my case. If they are absolute you can remove this
 +
local basePath = _T("D:\\ddddd\\src\\");
 +
for(local i = 0; i < fileList.GetCount(); i++)
 +
{
 +
        // Create file path
 +
local filePath = basePath + fileList.Item(i);
 +
// Add to all build targets
 +
for(local a = 0; a <= p.GetBuildTargetsCount(); a++)
 +
    pm.AddFileToProject(filePath, p, a);
 +
}
 +
// Save the project. This triggers also an update of the project UI
 +
pm.SaveAllProjects();
 +
</nowiki>
 +
</pre>
 +
 +
== See also ==
 +
 +
* [[Scripting Code::Blocks|Scripting index]]

Latest revision as of 12:19, 12 May 2020


Conditional compiler flags for different OS

For example different libraries for X11 and Windows

1) Project->Build options->Select the project name on the left

2) Linker Settings->Other linker options

3) Add the squirrel script:

[[if(PLATFORM == PLATFORM_MSW ) print(_("-lgdi32")); else if(PLATFORM == PLATFORM_X11 ) printf(_("-lX11"));]]

be aware, this has to be in one line of the other liner options...

After build

Copy the build log after compilation to a new location and rename it with target name and compiler id.

IO.CopyFile(_("$(PROJECTNAME)_build_log.html"), _("$(PROJECTNAME)_build_log_") + GetProjectManager().GetActiveProject().GetCurrentlyCompilingTarget().GetTitle() + _("_") + GetProjectManager().GetActiveProject().GetCurrentlyCompilingTarget().GetCompilerID() + _(".html"), true );

This is in one line, so you can copy and paste it in the post build step (but don't forget to add the [[ ]] )


Add files from a text file to the active project


local pm = GetProjectManager();
if (IsNull(pm))
{
    ShowError(_T("Could not query the project manager. Cannot continue."));
    return 0;
}

local p = pm.GetActiveProject();

// File with the source files separated by \n
// for example:
// '''''''''''''''''''
// a.cpp
// b.cpp
// '''''''''''''''''''
local filecontent = IO.ReadFileContents (_T("D:\\ddddd\\fileList.txt"));

// Split the files in lines
local fileList = GetArrayFromString(filecontent, _T("\n"), true);
print(_T("Files to load:"));
for(local i = 0; i < fileList.GetCount(); i++)
{
	print(fileList.Item(i) + _T("\n"));   // Print the files in the list, as a check
}

// Base path, the files in the text files are relative in my case. If they are absolute you can remove this
local basePath = _T("D:\\ddddd\\src\\");
for(local i = 0; i < fileList.GetCount(); i++)
{
        // Create file path
	local filePath = basePath + fileList.Item(i);
	// Add to all build targets
	for(local a = 0; a <= p.GetBuildTargetsCount(); a++)
	    pm.AddFileToProject(filePath, p, a);
}
// Save the project. This triggers also an update of the project UI
pm.SaveAllProjects();

See also