How can I use qmake to copy files recursively - qmake

In my source tree I have a bunch of resources, I want to copy them with make install to my defined target path. Since the resource tree has many, many subdirectories, I want qmake to find all files recursively.
I tried:
resources.path = /target/path
resources.files += `find /path/to/resources`
INSTALLS += resources
and:
resources.path = /target/path
resources.files += /path/to/resources/*
resources.files += /path/to/resources/*/*
resources.files += /path/to/resources/*/*/*
resources.files += /path/to/resources/*/*/*/*
INSTALLS += resources
Both don't have the result I was hoping for.

I have done it like this:
res.path = $$OUT_PWD/targetfolder
res.files = sourcefolder
INSTALLS += res
this would copy "wherever this qmake script is"/sourcefolder into buildfolder/"same sub folder on build dir"/targetfolder
so you would have targetfolder/sourcefolder/"all other subfolders and files..."
Example:
#(My .pro file's dir) $$PWD = /mysources/
#(My Build dir) $$OUT_PWD = /project_build/
extras.path = $$OUT_PWD
extras.files += extras
src.path = $$OUT_PWD
src.files += src
INSTALLS += extras src
Would copy /mysources/extras to /project_build/extras and /mysources/src to /project_build/src

It appears that directories are installed with 'cp -r -f', so this does the trick:
resources.path = /target/path
resources.files += /path/to/resources/dir1
resources.files += /path/to/resources/dir2
resources.files += /path/to/resources/dir3
resources.files += /path/to/resources/dirn # and so on...
resources.files += /path/to/resources/* # don't forget the files in the root
INSTALLS += resources

Related

How to create a py_library from a generated source tree

I am trying to consume some Python code + C extensions that are produced by CMake.
I'm using rules_foreign_cc to build the code, and it puts the code into an output directory, but I'm stumped as to how I can turn that into a py_library. I tried the below:
Depend on the cmake rule as srcs
Set imports to try to point to the directory containing the Python packages
But it doesn't work. When I run the py_test, the constructed PYTHONPATH doesn't point to the directory containing the python package from the cmake rule.
Thanks for any help!
Here's what I've got in my third_party/BUILD.bazel:
load("#rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
load("#rules_python//python:defs.bzl", "py_library", "py_test")
filegroup(
name = "torch_mlir_src",
srcs = glob(["torch-mlir/**"]),
)
make(
name = "torch_mlir",
...
install = False,
lib_source = ":torch_mlir_src",
out_data_dirs = ["python_packages"],
postfix_script = " && ".join([
"cp -r --dereference tools/torch-mlir/python_packages/torch_mlir $$INSTALLDIR$$/python_packages",
]),
targets = ["tools/torch-mlir/all"],
working_directory = "external/llvm-project/llvm",
)
# This doesn't seem to work.
py_library(
name = "torch_mlir_py",
srcs = [":torch_mlir"],
imports = ["$(BINDIR)/third_party/torch_mlir/python_packages"],
srcs_version = "PY3",
)
py_test(
name = "torch_mlir_annotations_sugar_test",
srcs = ["torch-mlir/python/test/annotations-sugar.py"],
main = "torch-mlir/python/test/annotations-sugar.py",
srcs_version = "PY3",
deps = [":torch_mlir_py"],
)

Bazel: how to get access to srcs of a filegroup?

I have some html files in "root/html_files/*.html" directory. I want to iterate on these html files and run some bazel rules on them, from "root/tests/" directory.
So, I made a filegroup of those html files and tried to get access to them in "root/tests/" directory, but that is't working.
I wonder if it is possible?
My BUILD file in "root/" directory:
HTMLS_LIST = glob(["html_files/*.html",])
filegroup(
name = "html_files",
srcs = HTMLS_LIST,
visibility = [ "//visibility:public" ],)
My BUILD file in "root/tests/" directory:
load("//tests:automation_test.bzl", "make_and_run_html_tests")
make_and_run_html_tests(
name = 'test_all_htmls',
srcs = ['test/automation_test.py'],
html_files = '//:html_files')
My bzl file in "root/tests/" directory:
def make_and_run_html_tests(name, html_files, srcs):
tests = []
for html_file in html_files: # I want to iterate on sources of html filegroup here
folders = html_file.split("/")
filename = folders[-1].split(".")[0]
test_rule_name = 'test_' + filename + '_file'
native.py_test(
name = test_rule_name,
srcs = srcs,
main = srcs[0],
data = [
html_file,
],
args = [html_file],
)
testname = ":" + test_rule_name
tests.append(testname)
native.test_suite(
name = name,
tests = tests,
)
And my python unittest file in "root/tests/" directory:
import sys
import codecs
import unittest
class TestHtmlDocumetns(unittest.TestCase):
def setUp(self):
self.html_file_path = sys.argv[1]
def test_html_file(self):
fid = codecs.open(self.html_file_path, 'r')
print(fid.read())
self.assertTrue(fid)
if __name__ == '__main__':
unittest.main(argv=[sys.argv[1]])
You can't access the references to the files inside another filegroup / rule from within a macro like that. You'd need to create a rule and access them via the ctx.files attr
However, you can iterate over them if you were to remove the filegroup, and pass the glob directly to the macro:
HTMLS_LIST = glob(["html_files/*.html"])
make_and_run_html_tests(
name = 'test_all_htmls',
srcs = ['test/automation_test.py'],
html_files = HTMLS_LIST
)
The glob is resolved to an array before expanding the macro

How to split a Torch class into several files in a Lua rock

In my recently aided in the development of a Dataframe package for Torch. As the code base has quickly doubled there is a need to split the class into several sections for better organization and follow-up (issue #8).
A simple test-class would be a test.lua file in the root folder of the test-package:
test = torch.class('test')
function test:__init()
self.data = {}
end
function test:a()
print("a")
end
function test:b()
print("b")
end
Now the rockspec for this would simply be:
package = "torch-test"
version = "0.1-1"
source = {
url = "..."
}
description = {
summary = "A test class",
detailed = [[
Just an example
]],
license = "MIT/X11",
maintainer = "Jon Doe"
}
dependencies = {
"lua ~> 5.1",
"torch >= 7.0",
}
build = {
type = 'builtin',
modules = {
["test"] = 'test.lua',
}
}
In order to get multiple files to work for a single class it is necessary to return the class object initially created and pass it to the subsections. The above example can be put into the file structure:
\init.lua
\main.lua
\test-0.1-1.rockspec
\Extensions\a.lua
\Extensions\b.lua
The luarocks install/make copies the files according to 'require' syntax where each . signifies a directory and the .lua is left out, i.e. we need to change the rockspec to:
package = "torch-test"
version = "0.1-1"
source = {
url = "..."
}
description = {
summary = "A test class",
detailed = [[
Just an example
]],
license = "MIT/X11",
maintainer = "Jon Doe"
}
dependencies = {
"lua ~> 5.1",
"torch >= 7.0",
}
build = {
type = 'builtin',
modules = {
["test.init"] = 'init.lua',
["test.main"] = 'main.lua',
["test.Extensions.a"] = 'a.lua',
["test.Extensions.b"] = 'b.lua'
}
}
The above will thus create a test-folder where the packages reside together with the files and subdirectories. The class initialization now resides in the init.lua that returns the class object:
test = torch.class('test')
function test:__init()
self.data = {}
end
return test
The subclass-files now need to pickup the class object that is passed using loadfile() (see init.lua file below). The a.lua should now look like this:
local params = {...}
local test = params[1]
function test:a()
print("a")
end
and similar addition for the b.lua:
local params = {...}
local test = params[1]
function test:b()
print("b")
end
In order to glue everything together we have the init.lua file. The following is probably a little over-complicated but it takes care of:
Finding all extensions available and loading them (Note: requires lua filesystem that you should add to the rockspec and you still need to add each file into the rockspec or it won't be in the Extensions folder)
Identifies the paths folder
Loads the main.lua
Works in a pure testing environment without the package installed
The code for init.lua:
require 'lfs'
local file_exists = function(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
-- If we're in development mode the default path should be the current
local test_path = "./?.lua"
local search_4_file = "Extensions/load_batch"
if (not file_exists(string.gsub(test_path, "?", search_4_file))) then
-- split all paths according to ;
for path in string.gmatch(package.path, "[^;]+;") do
-- remove trailing ;
path = string.sub(path, 1, string.len(path) - 1)
if (file_exists(string.gsub(path, "?", "test/" .. search_4_file))) then
test_path = string.gsub(path, "?", "test/?")
break;
end
end
if (test_path == nil) then
error("Can't find package files in search path: " .. tostring(package.path))
end
end
local main_file = string.gsub(test_path,"?", "main")
local test = assert(loadfile(main_file))()
-- Load all extensions, i.e. .lua files in Extensions directory
ext_path = string.gsub(test_path, "[^/]+$", "") .. "Extensions/"
for extension_file,_ in lfs.dir (ext_path) do
if (string.match(extension_file, "[.]lua$")) then
local file = ext_path .. extension_file
assert(loadfile(file))(test)
end
end
return test
I hope this helps if you run into the same problem and find the documentation a little too sparse. If you happen to know a better solution, please share.

How tell qmake NOT to create a folder?

I want to configurate my qmake so it will make my executables go under ./build/debug (or release). I've done that sucessfully with the following code:
CONFIG(debug, debug|release) {
DESTDIR = ./build/debug
TARGET = mShareLibd
}
CONFIG(release, debug|release) {
DESTDIR = ./build/release
TARGET = mShareLib
}
Everything works fine apart from the fact that qmake still creates two folders, namely "debug" and "release" in the project's root directory - so I end up with a "build", a "debug" (always empty) and a "release" (always empty) folder.
How can I tell qmake NOT to create this two folders? I did this question in the QtCentre forum (here is the link), but the way provided didn't seem to me to be a reasonable one. Isn't there a more reasonable approach - such as just write a command which tells "qmake, don't create this folders"?
Thanks,
Momergil
EDIT
Bill asked me to copy and paste my .pro file here. Here are the resumed version (most of the header and source files not included)
#qmake defines
MSHARE_REPO = $${PWD}/..
MSHARE_COMMON = $${MSHARE_REPO}/Common
MSHARE_LIB = $${MSHARE_REPO}/mShareLib
MLOGGER = $${MSHARE_REPO}/../Classes/mLogger
#inclusion
QT += core gui network multimedia sql
qtHaveModule(printsupport): QT += printsupport
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += qwt
#CONFIG *= precompile_header
#PRECOMPILED_HEADER = stdafx.h
#HEADERS += stdafx.h
TARGET = mShare
TEMPLATE = app
VER_MAJ = 0
VER_MIN = 0
VER_PAT = 7
VERSION = $${VER_MAJ}.$${VER_MIN}.$${VER_PAT}
INCLUDEPATH += MSHARE_REPO \
MSHARE_COMMON \
C:\Qt\Qwt-6.1.0\include
LIBS += $${PWD}/SMTPEmail.dll
DEFINES += MGENERALDEFINES_GUI \
MGENERALDEFINES_DEBUG \
MGENERALDEFINES_GENERAL \
QWT_INCLUDED \
APP_VERSION=\\\"$$VERSION\\\"
win32 {
LIBS += -lpsapi
CONFIG(debug, debug|release) { #debug {
LIBS += C:/Qt/Qwt-6.1.0/lib/qwtd.dll \
$${MLOGGER}/build/debug/mLogger.dll \ #$${MLOGGER}/debug/mLoggerd.dll \
$${MSHARE_LIB}/build/debug/mShareLibd.dll
DEFINES += DEBUG
DESTDIR = ./build/debug
}
CONFIG(release, debug|release) { #release {
LIBS += C:/Qt/Qwt-6.1.0/lib/qwt.dll \
$${MLOGGER}/build/release/mLogger.dll \
$${MSHARE_LIB}/build/release/mShareLib.dll
DEFINES += RELEASE \
QT_NO_DEBUG \
QT_NO_DEBUG_OUTPUT
DESTDIR = ./build/release
}
} # win32
#others
MOC_DIR = $${DESTDIR}/.moc
OBJECTS_DIR = $${DESTDIR}/.obj
UI_DIR = $${DESTDIR}/.ui
RCC_DIR = $${DESTDIR}/.rcc
########################################################################
HEADERS += AppDefines.hpp \
mreadwrite.hpp \
system/appbrain.hpp \
...
SOURCES += main.cpp \
mreadwrite.cpp \
system/appbrain.cpp \
...
FORMS += \
interface/entracedialog.ui \
interface/validationdialog.ui \
...
OTHER_FILES += Files/CandlePatternProbabilities.txt \
Project_Files/Readme.txt \
...
RESOURCES += \
Icons.qrc \
Setups.qrc \
GeneralFiles.qrc
RC_FILE = icone.rc
#TRANSLATIONS += DEFAULT_THEME_PATH/translations/app_pt.ts \
# DEFAULT_THEME_PATH/translations/app_de.ts
I think I've found the solution by looking at the QMake's source code : set the "PRECOMPILED_DIR" variable.
It works with Qt 5. Since the QMake source code doesn't change a lot, I think it also works with Qt 4.
CONFIG(debug, debug|release) {
DESTDIR = ./build/debug
PRECOMPILED_DIR = ./build/debug
TARGET = mShareLibd
}
CONFIG(release, debug|release) {
DESTDIR = ./build/release
PRECOMPILED_DIR = ./build/release
TARGET = mShareLib
}

How to make qmake to do a clean rebuild if DEFINES are changed

... which it should do but does not.
This is one of the major frustration of the qmake for me. qbs is our Qt future but for now we are stuck with qmake. So, what can be done?
I abuse QMAKE_EXTRA_COMPILERS to accompilsh this. I need to use it because I have to get DEFINES value after all features are processed.
# in this function all the work is done
defineReplace(checkDefinesForChanges) {
old_def = $$cat($$OUT_PWD/defines.txt)
curr_def = $$DEFINES
curr_def -= $$old_def
old_def -= $$DEFINES
diff = $$old_def $$curr_def
# delete all files in OUT_PWD if macros were changed
!isEmpty(diff) {
A = $$system(del /F /Q /S $$system_path($${OUT_PWD}/*.*))
message(DEFINES WERE CHANGED)
}
write_file($$OUT_PWD/defines.txt, DEFINES);
return(???)
}
# use QMAKE_EXTRA_COMPILERS to launch function
# checkDefinesForChanges after all features processing
_defines_check_ = ???
defines_check.name = check on defines being changed
defines_check.input = _defines_check_
defines_check.CONFIG += no_link ignore_no_exist
defines_check.depends = ???
defines_check.commands = ???
defines_check.output_function = checkDefinesForChanges
defines_check.clean = 333
QMAKE_EXTRA_COMPILERS += defines_check
# make sure qmake is run if deines.txt is deleted
recompile_on_defines_txt_not_existsing.target = $(MAKEFILE)
recompile_on_defines_txt_not_existsing.depends = $$OUT_PWD/defines.txt
recompile_on_defines_txt_not_existsing2.target = $$OUT_PWD/defines.txt
recompile_on_defines_txt_not_existsing2.depends = qmake
QMAKE_EXTRA_TARGETS += recompile_on_defines_txt_not_existsing recompile_on_defines_txt_not_existsing2
Source in Russian

Resources