[lttng-dev] [PATCH] Implement the .o file generation in lttng-gen-tp

Yannick Brosseau yannick.brosseau at gmail.com
Thu Feb 16 16:48:12 EST 2012


(refs #40)

Signed-off-by: Yannick Brosseau <yannick.brosseau at gmail.com>
---
 doc/examples/gen-tp/Makefile |   19 +++++++++--
 doc/man/lttng-gen-tp.1       |   24 +++++++++++---
 tools/lttng-gen-tp           |   72 ++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 104 insertions(+), 11 deletions(-)
 mode change 100644 => 100755 tools/lttng-gen-tp

diff --git a/doc/examples/gen-tp/Makefile b/doc/examples/gen-tp/Makefile
index 664372f..c78fc67 100644
--- a/doc/examples/gen-tp/Makefile
+++ b/doc/examples/gen-tp/Makefile
@@ -20,17 +20,30 @@ LIBS = -ldl -llttng-ust
 
 all: sample
 
-sample: sample.o sample-tp.o
+sample: sample.o sample_tracepoint.o
 	$(CC) $(LIBS) -o $@ $^
 
 sample.o: sample.c sample_tracepoint.h
 	$(CC) $(CFLAGS) -c -o $@ $<
 
-sample-tp.o: sample_tracepoint.c sample_tracepoint.h
-	$(CC) $(CFLAGS) -I. -c -o $@ $<
+# Use this command to compile the .c manually
+#sample_tracepoint.o: sample_tracepoint.c sample_tracepoint.h
+#	$(CC) $(CFLAGS) -I. -c -o $@ $<
+
+# This rule generate .o only and depends on rules for generating
+# the .h and .c
+%.o: %.tp %.c %.h
+	tools/lttng-gen-tp -o $@ $<
+
+# The following rule can be used to generate all files instead of having one
+# for each file type. Note that the sample.o has a dependency on the
+# .h, so you need to change that if you remove the %.h rule.
+#%.o: %.tp
+#	lttng-gen-tp $<
 
 %.h: %.tp
 	lttng-gen-tp -o $@ $<
+
 %.c: %.tp
 	lttng-gen-tp -o $@ $<
 
diff --git a/doc/man/lttng-gen-tp.1 b/doc/man/lttng-gen-tp.1
index 3c7034c..551f003 100644
--- a/doc/man/lttng-gen-tp.1
+++ b/doc/man/lttng-gen-tp.1
@@ -21,10 +21,12 @@ The \fBlttng\-gen\-tp\fP tool simplify the generation of the UST tracepoint
 files. It takes a simple template file and generate the necessary code to use the defined tracepoints in your application.
 The section TEMPLATE FILE FORMAT describe the content of the template file.
 
-Currently, the tool can generate the .h and .c associated to your
+Currently, the tool can generate the .h, .c and .o associated to your
 tracepoint. The generated .h can be directly included in your application.
-You need to compile the .c into a .o, .a or .so at your choice and
-link it with your application. Refer to the UST documentation for the
+You can let the tool generate the .o or compile the .c yourself.
+You can compile the .c into a .o, .a or .so at your choice and
+link it with your application.
+Refer to the UST documentation for the
 advantages and disadvantage of each form.
 To compile the resulting .c file, you need to add the options
 "-llttng-ust -I."
@@ -45,11 +47,11 @@ Increase verbosity.
 .TP
 .BR "\-o, \-\-output"
 Specify the generated file. The type of the generated file depend on the file
-extension (.h, .c).
+extension (.h, .c, .o).
 This option can be specfied multiple times to generate different file type.
 
 .PP
-When no output is specified de default files are generated with the same base filename as the template file. The default files are: .h, .c.
+When no output is specified de default files are generated with the same base filename as the template file. The default files are: .h, .c, .o.
 
 .SH "TEMPLATE FILE FORMAT"
 
@@ -77,7 +79,19 @@ TRACEPOINT_EVENT(
         ctf_string(message, text)
     )
 )
+.SH "ENVIRONMENT VARIABLES"
 
+.PP
+When the tool generate an .o file, it will look for the following environment variables
+.PP
+
+.PP
+.IP "CC"
+Specifer which C compiler to use. If the variable is not specified, the
+tool will try "cc" and "gcc"
+
+.IP "CFLAGS"
+Flags directly passed to the compiler
 .SH "SEE ALSO"
 
 .PP
diff --git a/tools/lttng-gen-tp b/tools/lttng-gen-tp
old mode 100644
new mode 100755
index 9b08275..3f28534
--- a/tools/lttng-gen-tp
+++ b/tools/lttng-gen-tp
@@ -19,6 +19,8 @@
 import sys
 import getopt
 import re
+import os
+import subprocess
 
 class Usage(Exception):
     def __init__(self, msg):
@@ -90,6 +92,59 @@ class CFile:
                                            headerFilename = headerFilename))
         outputFile.close()
 
+class ObjFile:
+    def __init__(self, filename, template):
+        self.outputFilename = filename
+        self.template = template
+    def _detectCC(self):
+        cc = ""
+        if os.environ.has_key('CC'):
+            cc = os.environ['CC']
+            try:
+                subprocess.call(cc,
+                                stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE)
+            except OSError, msg:
+                print "Invalid CC environment variable"
+                cc = ""
+
+        else:
+            # Try c first, if that fails try gcc
+            try:
+                useCC = True
+                subprocess.call("cc",
+                                stdout=subprocess.PIPE,
+                                stderr=subprocess.PIPE)
+            except OSError, msg:
+                useCC = False
+            if useCC:
+                cc = "cc"
+
+            else:
+                try:
+                    useGCC = True
+                    subprocess.call("gcc",
+                                    stdout=subprocess.PIPE,
+                                    stderr=subprocess.PIPE)
+                except OSError, msg:
+                    useGCC = False
+                if useGCC:
+                    cc = "gcc"
+        return cc
+
+    def write(self):
+        cFilename = self.outputFilename.replace(".o",".c")
+        cc = self._detectCC()
+        if cc == "":
+            raise RuntimeError("No C Compiler detected")
+        if os.environ.has_key('CFLAGS'):
+            cflags = os.environ['CFLAGS']
+        else:
+            cflags = ""
+
+        command = cc + " -c " + cflags + " -I. -llttng-ust" + " -o " + self.outputFilename + " " + cFilename
+        subprocess.call(command.split())
+
 class TemplateFile:
     def __init__(self, filename):
         self.domain = ""
@@ -132,9 +187,9 @@ usage="""
 
  If no OUTPUT_FILE is given, the .h and .c file will be generated.
  (The basename of the template file with be used for the generated file.
-  for example sample.tp will generate sample.h and sample.c)
+  for example sample.tp will generate sample.h, sample.c and sample.o)
 
- When using the -o option, the OUTPUT_FILE must end with either .h or .c
+ When using the -o option, the OUTPUT_FILE must end with either .h, .c or .o
  The -o option can be repeated multiple times.
 
  The template file must contains TRACEPOINT_EVENT and TRACEPOINT_LOGLEVEL
@@ -168,8 +223,10 @@ def main(argv=None):
 
     doCFile = None
     doHeader = None
+    doObj = None
     headerFilename = None
     cFilename = None
+    objFilename = None
 
     if len(outputNames) > 0:
         if len(args) > 1:
@@ -184,13 +241,15 @@ def main(argv=None):
                 doCFile = True
                 cFilename = outputName
             elif outputName[-2:] == ".o":
-                print "Not yet implemented, sorry"
+                doObj = True
+                objFilename = outputName
             else:
                 print "output file type unsupported"
                 return(4)
     else:
         doHeader = True
         doCFile = True
+        doObj = True
 
     # process arguments
     for arg in args:
@@ -210,6 +269,13 @@ def main(argv=None):
                 curFilename = re.sub("\.tp$",".c",arg)
             dotc = CFile(curFilename, tpl)
             dotc.write()
+        if doObj:
+            if objFilename:
+                curFilename = objFilename
+            else:
+                curFilename = re.sub("\.tp$",".o",arg)
+            dotobj = ObjFile(curFilename, tpl)
+            dotobj.write()
 
 if __name__ == "__main__":
     sys.exit(main())
-- 
1.7.9




More information about the lttng-dev mailing list