9d060d907827f52974e73a6b04629ea5245c0c46
[gnome.gobject-introspection] / Makefile.introspection
1 # -*- Mode: make -*-
2 # Copyright 2009 Johan Dahlin
3 #
4 # This file is free software; the author(s) gives unlimited
5 # permission to copy and/or distribute it, with or without
6 # modifications, as long as this notice is preserved.
7 #
8 # * Input variables:
9 #
10 #   INTROSPECTION_GIRS - List of GIRS that should be generated
11 #   INTROSPECTION_SCANNER - Command to invoke scanner, normally set by
12 #      GOBJECT_INTROSPECTION_REQUIRE/CHECK() in introspection.m4
13 #   INTROSPECTION_SCANNER_ARGS - Additional args to pass in to the scanner
14 #   INTROSPECTION_COMPILER - Command to invoke compiler, normally set by
15 #      GOBJECT_INTROSPECTION_REQUIRE/CHECK() in introspection.m4
16 #   INTROSPECTION_COMPILER_ARGS - Additional args to pass in to the compiler
17 #
18 # * Simple tutorial
19 #
20 # Add this to configure.ac:
21 #   -Wno-portability to AM_INIT_AUTOMAKE
22 #   GOBJECT_INTROSPECTION_CHECK([0.6.7])
23 #
24 # Add this to Makefile.am where your library/program is built:
25 #   include $(INTROSPECTION_MAKEFILE)
26 #   INTROSPECTION_GIRS = YourLib-1.0.gir
27 #   YourLib_1_0_gir_NAMESPACE = YourLib
28 #   YourLib_1_0_gir_VERSION = 1.0
29 #   YourLib_1_0_gir_LIBS = libyourlib.la
30 #   YourLib_1_0_gir_FILES = $(libyourlib_1_0_SOURCES)
31 #   girdir = $(INTROSPECTION_GIRDIR)
32 #   dist_gir_DATA = YourLib-1.0.gir
33 #   typelibdir = $(INTROSPECTION_TYPELIBDIR)
34 #   typelib_DATA = YourLib-1.0.typelib
35 #   CLEANFILES = $(dist_gir_DATA) $(typelib_DATA)
36 #
37
38 # Make sure the required variables are set, these should under normal
39 # circumstances come from introspection.m4
40 $(if $(INTROSPECTION_GIRS),,$(error Need to define INTROSPECTION_GIRS))
41 $(if $(INTROSPECTION_SCANNER),,$(error Need to define INTROSPECTION_SCANNER))
42 $(if $(INTROSPECTION_COMPILER),,$(error Need to define INTROSPECTION_COMPILER))
43
44 #
45 # Creates a GIR by scanning C headers/sources
46 # $(1) - Name of the gir file (output)
47 #
48 # If output is Gtk-2.0.gir then you should name the variables like
49 # Gtk_2_0_gir_NAMESPACE, Gtk_2_0_gir_VERSION etc.
50 # Required variables:
51 # FILES - C sources and headers which should be scanned
52 #
53 # One of these variables are required:
54 # LIBS - Library where the symbol represented in the gir can be found
55 # PROGRAM - Program where the symbol represented in the gir can be found
56 #
57 # Optional variables
58 # NAMESPACE - Namespace of the gir, first letter capital,
59 #   rest should be lower case, for instance: 'Gtk', 'Clutter', 'ClutterGtk'.
60 #   If not present the namespace will be fetched from the gir filename,
61 #   the part before the first dash. For 'Gtk-2.0', namespace will be 'Gtk'.
62 # VERSION - Version of the gir, if not present, will be fetched from gir
63 # filename, the part after the first dash. For 'Gtk-2.0', version will be '2.0'.
64 # LIBTOOL - Command to invoke libtool, usually set by automake
65 # SCANNERFLAGS - Flags to pass in to the scanner, see g-ir-scanner(1) for a list
66 # CFLAGS - Flags to pass in to the parser when scanning headers
67 # PACKAGES - list of pkg-config names which cflags are required to parse
68 #   the headers of this gir
69 # INCLUDES - Gir files to include without the .gir suffix, for instance
70 #   GLib-2.0, Gtk-2.0. This is needed for all libraries which you depend on that
71 #   provides introspection information.
72 #
73 define introspection-scanner
74
75 # Transform the gir filename to something which can reference through a variable
76 # without automake/make complaining, eg Gtk-2.0.gir -> Gtk_2_0_gir
77 _gir_name = $(subst -,_,$(subst .,_,$(1)))
78
79 # Basic sanity check, to make sure required variables are set
80 $(if $($(_gir_name)_FILES),,$(error Need to define $(_gir_name)_FILES))
81 $(if $(or $($(_gir_name)_LIBS),
82           $($(_gir_name)_PROGRAM)),,
83     $(error Need to define $(_gir_name)_LIBS or $(_gir_name)_PROGRAM))
84
85 # Namespace and Version is either fetched from the gir filename
86 # or the _NAMESPACE/_VERSION variable combo
87 _gir_namespace = $(or $($(_gir_name)_NAMESPACE),$(firstword $(subst -, ,$(1))))
88 _gir_version = $(or $($(_gir_name)_VERSION),$(lastword $(subst -, ,$(1:.gir=))))
89
90 # _PROGRAM is an optional variable which needs it's own --program argument
91 _gir_program = $(if $($(_gir_name)_PROGRAM),--program=$($(_gir_name)_PROGRAM))
92
93 # Variables which provides a list of things
94 _gir_libraries = $(foreach lib,$($(_gir_name)_LIBS),--library=$(lib))
95 _gir_packages = $(foreach pkg,$($(_gir_name)_PACKAGES),--pkg=$(pkg))
96 _gir_includes = $(foreach include,$($(_gir_name)_INCLUDES),--include=$(include))
97
98 # Reuse the LIBTOOL variable from by automake if it's set
99 _gir_libtool = $(if $(LIBTOOL),--libtool="$(LIBTOOL)")
100
101 # Only dependencies we know are actually filenames goes into _FILES, make
102 # sure these are built before running the scanner. Libraries and programs
103 # needs to be added manually.
104 $(1): $$($(_gir_name)_FILES) $(INTROSPECTION_PARSER)
105     ## Invoke the scanner
106         $(INTROSPECTION_SCANNER) $(INTROSPECTION_SCANNER_ARGS) \
107           --namespace=$(_gir_namespace) \
108           --nsversion=$(_gir_version) \
109           $(_gir_libtool) \
110           $(_gir_program) \
111           $(_gir_libraries) \
112           $(_gir_packages) \
113           $(_gir_includes) \
114           $($(_gir_name)_SCANNERFLAGS) \
115           $($(_gir_name)_CFLAGS) \
116           $($(_gir_name)_FILES) \
117           ## Output should go last as it makes it easier to read compilation logs
118           --output $(1)
119 endef
120
121 $(foreach gir,$(INTROSPECTION_GIRS),$(eval $(call introspection-scanner,$(gir))))
122
123 #
124 # Compiles a gir into a typelib
125 # $(1): gir filename (input)
126 # $(2): typelib filename (output)
127 #
128 define introspection-compiler
129 $(INTROSPECTION_COMPILER) $(INTROSPECTION_COMPILER_ARGS) --includedir=. $(1) -o $(2)
130 endef
131
132 # Simple rule to compile a typelib.
133 %.typelib: %.gir $(INTROSPECTION_COMPILER)
134         $(call introspection-compiler,$<,$@)