Author Topic: create project given list of source files  (Read 583 times)

Offline Beliavsky

  • Single posting newcomer
  • *
  • Posts: 2
create project given list of source files
« on: January 08, 2018, 08:46:05 pm »
I have a text file containing list of Fortran source files than can be compiled into an executable using gfortran. Can I create create a project file by importing this list rather than typing the source file names manually?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 11126
    • Travis build status
Re: create project given list of source files
« Reply #1 on: January 08, 2018, 09:23:23 pm »
Nope, but you can write a script which adds the files from the list to an empty project.

See here for the apis: http://wiki.codeblocks.org/index.php/Scripting_commands#ProjectManager
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Beliavsky

  • Single posting newcomer
  • *
  • Posts: 2
Re: create project given list of source files
« Reply #2 on: January 08, 2018, 11:53:55 pm »
This Python script

infile = "list.txt"
lines = open(infile,"r").readlines()
print('"' + '" "'.join([line.strip() for line in lines]) + '"')

gives output such as

"foo.f90" "bar.f90" "main.f90"

which can then be pasted in the "File name:" box that appears when one clicks Project, add files.

Offline BlueHazzard

  • Lives here!
  • ****
  • Posts: 2022
Re: create project given list of source files
« Reply #3 on: January 09, 2018, 02:11:30 pm »
Codeblocks uses squirrel for scripting:  http://squirrel-lang.org/ it is close to lua, but with c like syntax.
You can run one line scripts with the script console: View->scrip console. In the bottom line you can enter commands, or running a .script file.

In your case i would run a script file with this content:
Code: [Select]
local arr =
[
"File 1.c",
"File 1.h",
"File 2.c",
"File 2.h"
];

local project = GetProjectManager().GetActiveProject();
foreach(i, fi in arr)
{
Log(_T(fi));
for(i = 0; i < project.GetBuildTargetsCount(); ++i)
GetProjectManager().AddFileToProject( _T(fi), project, i);
};
GetProjectManager().SaveProject(project);
Log(_T("Import finished"));

Edit: For the above script all files have to exist and be in the same folder as the project but you can use relative paths and absolute paths. To read a txt file ask or read the squirrel documentation
Edit2: The files are getting added to all targets of the project
« Last Edit: January 09, 2018, 02:20:35 pm by BlueHazzard »