module pkgconfig
import c
+private import annotation
+private import literal
redef class ToolContext
- var pkgconfig_phase: Phase = new PkgconfigPhase(self, null)
+ var pkgconfig_phase: Phase = new PkgconfigPhase(self, [literal_phase])
end
# Detects the `pkgconfig` annotation on the module declaration only.
redef fun process_annotated_node(nmoduledecl, nat)
do
# Skip if we are not interested
- if nat.n_atid.n_id.text != "pkgconfig" then return
+ if nat.name != "pkgconfig" then return
# Do some validity checks and print errors if the annotation is used incorrectly
var modelbuilder = toolcontext.modelbuilder
return
end
- var args = nat.n_args
- if args.is_empty then
- modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects at least one argument.")
- return
- end
+ # retreive module
+ var nmodule = nmoduledecl.parent.as(AModule)
+ var mmodule = nmodule.mmodule.as(not null)
+ # target pkgs
var pkgs = new Array[String]
- for arg in args do
- if not arg isa AExprAtArg then
- modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
- return
- end
- var expr = arg.n_expr
- if not expr isa AStringFormExpr then
- modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
- return
+ var args = nat.n_args
+ if args.is_empty then
+ # use module name
+ pkgs.add(mmodule.name)
+ else
+ for arg in args do
+ var pkg = arg.as_string
+ if pkg == null then
+ modelbuilder.error(nat, "Syntax error: \"pkgconfig\" expects its arguments to be the name of the package as String literals.")
+ return
+ end
+
+ pkgs.add(pkg)
end
-
- var pkg = expr.collect_text
- pkg = pkg.substring(1, pkg.length-2)
- pkgs.add(pkg)
end
- # retreive module
- var nmodule = nmoduledecl.parent.as(AModule)
-
# check availability of pkg-config
var proc_which = new IProcess("which", "pkg-config")
proc_which.wait
# compiler
var proc = new IProcess("pkg-config", "--cflags", pkg)
var compiler_opts = proc.read_all
- nmodule.c_compiler_options = "{nmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
+ mmodule.c_compiler_options = "{mmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
# linker
proc = new IProcess("pkg-config", "--libs", pkg)
var linker_opts = proc.read_all
- nmodule.c_linker_options = "{nmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
+ mmodule.c_linker_options = "{mmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
end
end