How to add all .pri files inside a directory - c++

I've searched quite thoroughly, but couldn't find a way to get all the folders inside a directory. I have the following qt project structure
first/
second/
- second.pri
third/
- third.pri
...
first.pri
I want to include all the pri files inside the first.pri file. I can do
include($$PWD/second/second.pri)
include($$PWD/third/third.pri)
...
But the problem is there could be many sub directories added in the future and I want to automatically include all the pri files. I've tried
FILES = $$files($$PWD/*/*.pri)
for(FILE, FILES) {
include($$FILE)
}
But this does not find the folders inside 'first' folder. $$files documentation allows specification of recursive, but it does not descend into subdirectories. How do I achieve it?

The following QMAKE function lists recursively all the files below a directory:
# - Function that list all files below a directory
defineReplace(list_recursively) {
FILES = $$files($$1)
RESULT =
for(var, $$list($$FILES)) {
exists($$var)
{
RESULT *= $$find(var, .*\.pri)
RESULT *= $$list_recursively($$var/*)
}
}
return($$RESULT)
}
# - Define the base directory
DIR = $$PWD/*
# - Test the function
temp_dir = $$list_recursively($$DIR)
# - Output the result
for(var, $$list($$temp_dir)) {
message($$var)
}
In the list_recursively function, it is possible to filter only those files with a specific extension, for example .pri. This is not even required if your project structure contain only .pri file.
Note: QMake does not work correctly with white spaces, it is usually a good idea to avoid them.
EDITED:
Added a filter for .pri files.

Related

Gradle include all files under a directory ('recursively')

So I've been making a Java project using Gradle (GitHub), but it uses/needs JNI (natives).
I was first using Makefiles to compile and link the C++ code, but then I found out how to compile and link C++ natives using Gradle, so I got all of that working. But now I am stuck, because I can't find a way to include all natives, on the same level (base) inside of the JAR file. NOTE: I don't need help with compiling the natives, only with including/packaging them.
EDIT: I just commited the changes to GitHub.
This is my directory structure:
[Project Directory]
- build.gradle
- src/
- win32/ (the only native library that i currently have)
- cpp/
- main/
- java/
- build/
- libs/ (here is the JAR and the natives)
- win32/ (the natives)
- shared/ (the dynamic link libraries, i only want these)
- x64/ (i want to include both x64 and x86)
- mylibrary.dll (just the DLLs should be included)
- mylibrary.ext
- mylibrary.lib
- x86/
So there are a few criteria:
I only want the DLL files, none of the .ext and .lib stuff.
I want to be able to dynamically change the amount of libraries and the names of the natives.
What I have tried:
My first attempt was just looping through all folders. I didn't have to use recursion because the depth of the file structure is fixed, so it will never be further from or closer to the build/libs directory. This is how I tried coding it:
sourceSets {
main {
resources {
srcDirs "src/main/resources"
// include natives
String libfp = "${buildDir}/libs/"
File libf = new File(libfp);
if (!libf.exists())
libf.mkdir();
FileFilter isDir = f -> f.isDirectory();
FileFilter isDll = f -> f.getAbsolutePath().endsWith(".dll");
for (File file : libf.listFiles(isDir)) { // iterate "libs" to find all libraries
// enter "shared"
File filen = new File(file.getAbsolutePath() + "/shared/");
for (File file1 : filen.listFiles(isDir)) { // iterate over "shared" to find all platforms
for (File file2 : file1.listFiles(isDll)) { // get all dlls
include(file2.getAbsolutePath())
}
}
}
}
}
}
This worked, except from the including itself. I don't know if I understand how this works correctly, but the include function didn't seem to add anything to the resources.
Then, I checked the documentation and found it was a pattern based function, so I tried making a simple include call with the pattern I thought would work:
include "/build/libs/**/*.dll"
// I also tried the following:
include "/build/libs/**.dll"
include "/build/libs/*.dll"
But that didn't seem to work too. So I think I am just misunderstanding how the include function works.
Just use
include '/build/libs/**'
will work. Thanks.

How to get files with a particular extension within a folder in python?

I'm working on a python code. When I change few parameters of run code every time it generates a new folder (containing files with npy extension) within main folder. I want to get access to all npy files in new folder. If I use
`os.path.listdir()`
it only lists the files in main folder.How I can approach files with npy extension?
you need to specify the path of the directory in your function
when you call:
`os.path.listdir()`
it is going to the current directory by default. If for example your new folder was called foobar then you would need to write it as something like:
path = "/foobar/"
os.listdir( path )
or equivelantly this should work also
os.listdir( "/foobar/" )
Let me know if you need any clarification
EDIT
If you want only files with the npy extension you can use the following code:
for file in os.listdir("/foobar"):
if file.endswith(".npy"):
print(os.path.join("/npy", file))
Again, let me know if you need clarification here.

move all .cpp & .h in a filter to another project AND correct their folder location

Here is filter of my project in Visual Studio shown in Solution Explorer :-
ProjectName1
== References, External Dependencies, Header Files, Resource Files
== Source Files
==== myFilter01
------ K.h (system folder = `D:\ProjectName1\K.h`)
------ K.cpp (system folder = `D:\ProjectName1\K.cpp`)
==== myFilter02
====== subFilter2_1
--------- B.h (system folder = `D:\ProjectName1\B.h`)
--------- B.cpp (system folder = `D:\ProjectName1\B.cpp`)
========= subFilter2_2
----------- C.h (system folder = `D:\ProjectName1\C.h`)
----------- C.cpp (system folder = `D:\ProjectName1\C.cpp`)
ProjectName2
== ... (some existing filter/files)
(In real case, all filters contains a lot of sub-sub-filter and files.)
Question
How to :
move all .h and .cpp files (B and C) inside myFilter02 to ProjectName2's folder (e.g. D:\ProjectName2)
don't change appearance of the filter (e.g. C must be still in subFilter2_1\subFilter2_2)
and do it in a few clicks (i.e. not depend on amount of files/sub-filters) i.e. O(1)
Here is the expected result :-
ProjectName1
== References, External Dependencies, Header Files, Resource Files
== Source Files
==== myFilter01
------ K.h (system folder = `D:\ProjectName1\K.h` )
------ K.cpp (system folder = `D:\ProjectName1\K.cpp` )
==== myFilter02
ProjectName2
== ... (some existing filter/files)
== subFilter2_1
----- B.h (system folder = `D:\ProjectName2\B.h` )
----- B.cpp (system folder = `D:\ProjectName2\B.cpp` )
===== subFilter2_2
------- C.h (system folder = `D:\ProjectName2\C.h` )
------- C.cpp (system folder = `D:\ProjectName2\C.cpp` )
It can be done manually for each sub-sub-sub-filter + add existing files, but it is very tedious.
I tried to right click the filter/files, but didn't found such feature.
I currently don't use any Microsoft's source control / repository (just in case it is related).
Note: The normal drag & drop on filters don't move the files to another project's folder.
It just makes the moved files to be a shortcut of the original location (D:\ProjectName1\).
Hotkey? Plugin? Script?
Do I really have to create a program to do this specific thing?
A few days after asking, I have coded it with c++ using RapidXML ~ 500-1000 lines.
I have to edit .vcxproj.filters and .vcxproj of both projects, and move some system files.
I still find no answer about the question, though.
Edit
(After receive advise from Hans Passant and Prab, thank!)
I want to use filter rather than folder for these reasons :-
Source control is easier, because all source files are in a same directory.
In Visual Studio, I can move files around different filters a little easier than around folders.
With Filter, I don't have to lengthen #include "../myFilter01/K.h" or add additional include directories for each folder. I can simple #include "K.h".
If I change the place where a file resides in a filter, I don't have to refactor it.
In case of changing folder, I have to refactor code.
I can use very strange character e.g. =◆██myFilter01██◆= for filters but not folder. It is my taste.
I don't want to
Use folder instead of filter : Beside difficulty of recreate many folders and move my .cpp/.h files manually, I will suffer the above disadvantages.
Use folder with same structure as filter : I have to keep it in sync together manually (all the time - tedious). I will still get some the above disadvantages.
In summary, using folder instead of filter causes me more new trouble than it solves.
It's not one click solution but it could be done faster then manual.
Find out what file need to be copied. To do that move filters from one project to another. Then switch to directory view (show all files) for first project and copy but not past files with small red indicator (files that are not included in the project). Now get back to filter view for both project and move back filters to first project. After that switch both projects to directory view and do pass on second project.
Now we need to create correct filters. Move files from source and header filter to another filters in second project. Unload both projects. Open first and second project .filters files copy from first file to second all filter tags except "Source Files", "Header Files", "Resource Files". Next from second file remove all tags started with source or header. Now from second ItemGroup copy whole content and pass it to second file second ItemGroup. Save second file.
In this point you should have copy of first project filter tree without unnecessary files now just move what you want to correct places.
It is helpful to create single tree in first project.

Create c++ NuGet package with nested include folders

I'm trying to package a toolkit for C++ where the include files are spread over several folders, like so:
Includes - cen_dcm -dcmnet
-ofstd
-dcmdata
Inside the nuspec is: include: { ${SDK_Base}\cen_dcm\**\*.h };
With this package deployed I get the include files in the following location:
..\packages\DCMTK.3.42.0.0\build\native\include including subfolders.
When I use the including the IntelliSense has no problems finding it, but if that include file includes something from a different folder it fails to find it.
So I use: #include"dcmnet/assoc.h" which works just fine, but when compiling assoc.h it reports it cannot find osconfig.h
That file is in the package but in the ofstd folder.
Normally I'd solve that by adding additional includes, but since this is a package I don't want that.
What am I missing? I can't imagine that support for something so basic is lacking?
To answer my own question. You can add {targets} to your autopkg file.
It looks like this:
targets {
Includes += "$(MSBuildThisFileDirectory)..\..${d_include}cen_dcm";
Includes += "$(MSBuildThisFileDirectory)..\..${d_include}cen_dcm\config";
Includes += "$(MSBuildThisFileDirectory)..\..${d_include}cen_dcm\dcmtls";
Includes += "$(MSBuildThisFileDirectory)..\..${d_include}cen_dcm\dcmdata";
Includes += "$(MSBuildThisFileDirectory)..\..${d_include}cen_dcm\dcmnet";
Includes += "$(MSBuildThisFileDirectory)..\..${d_include}cen_dcm\ofstd";
}
This additional include folders will show up in the of your package.targets file.
There are more possibilities using {targets}. See http://coapp.org/reference/autopackage-ref.html

Waf: Recursively collect source files and include paths

My C-gcc project structure is:
\Project\wscript (only one in project)
\Project\build\
\Project\Source\Module_1\foo.c
\Project\Source\Module_1\foo.h
\Project\Source\Module_1\dummy\foo2.h
\Project\Source\Module_n\bar.c
\Project\Source\Module_n\any dept...\bar.h
How can I recursively find all *.C files in 'Source' and add to
bld.program(source=HERE)?
instead of manually listing it:
bld.program(source="foo.c bar.c...", includes='..\\Source ..\\Source\Module_1')
Also how can I find every subfolders (preferably which has *.h) and append to include path?
Should I write my own finder functions in python and just pass it?
bld.program(source=SRCs_FOUND, includes=Paths_FOUND)
Will this cause any dependency problems in building?
In any modern IDE this thinking is common, drag one file to the Source tree, and it's automatically added to the build list. Thanks!
You can use globbing to scan the directories.
bld.program(
name = ...
....
source = bld.path.ant_glob('**/Source/*.C')
)
Just search for ant_glob in the waf book.