diff --git a/blueprint-compiler.py b/blueprint-compiler.py index a3eb0e9..aa1dc8f 100755 --- a/blueprint-compiler.py +++ b/blueprint-compiler.py @@ -21,18 +21,19 @@ import os, sys -# Try to find the python module, assuming the current file is installed to (prefix)/bin -dirname = os.path.join(os.path.dirname(os.path.dirname(__file__)), "share", "blueprint-compiler") -if os.path.isdir(os.path.join(dirname, "blueprintcompiler")): - sys.path.insert(0, dirname) - -# Get the configured (or, if running from source, not configured) version number +# These variables should be set by meson. If they aren't, we're running +# uninstalled, and we might have to guess some values. version = "@VERSION@" +module_path = "@MODULE_PATH@" +libdir = "@LIBDIR@" -def literal(key): - return "@" + key + "@" +if version == "\u0040VERSION@": + version = "uninstalled" +else: + # If Meson set the configuration values, insert the module path it set + sys.path.insert(0, module_path) from blueprintcompiler import main if __name__ == "__main__": - main.main("uninstalled" if version == literal("VERSION") else version) + main.main(version) diff --git a/meson.build b/meson.build index bd6fba2..540c0ec 100644 --- a/meson.build +++ b/meson.build @@ -17,22 +17,29 @@ configure_file( install_dir: join_paths(datadir, 'pkgconfig'), ) +config = configuration_data({ + 'VERSION': meson.project_version(), + 'LIBDIR': get_option('prefix') / get_option('libdir'), +}) + +if meson.is_subproject() + config.set('MODULE_PATH', meson.current_source_dir()) +else + config.set('MODULE_PATH', py.get_install_dir()) +endif + blueprint_compiler = configure_file( input: 'blueprint-compiler.py', output: 'blueprint-compiler', - configuration: { - 'VERSION': meson.project_version(), - }, + configuration: config, install: not meson.is_subproject(), install_dir: get_option('bindir'), ) -# Don't use the output configure_file here--that file is in the build directory -# and won't be able to find the python modules in the source directory. -meson.override_find_program('blueprint-compiler', find_program('blueprint-compiler.py')) - -if not meson.is_subproject() - install_subdir('blueprintcompiler', install_dir: datadir / 'blueprint-compiler') +if meson.is_subproject() + meson.override_find_program('blueprint-compiler', blueprint_compiler) +else + install_subdir('blueprintcompiler', install_dir: py.get_install_dir()) endif subdir('tests')