Browse Source

adding coverage option for Makefile and setup.py

Summary: coverage option allows to compile in coverage mode in order to get execution metrics

Reviewed By: EdouardGrave

Differential Revision: D11659859

fbshipit-source-id: 0d831571e00fadf2002d6b074a89ff76fa7dcfe1
Onur Çelebi 7 years ago
parent
commit
0ddcd5f09d
2 changed files with 21 additions and 2 deletions
  1. 4 1
      Makefile
  2. 17 1
      setup.py

+ 4 - 1
Makefile

@@ -15,6 +15,9 @@ INCLUDES = -I.
 opt: CXXFLAGS += -O3 -funroll-loops
 opt: fasttext
 
+coverage: CXXFLAGS += -O0 -fno-inline -fprofile-arcs --coverage
+coverage: fasttext
+
 debug: CXXFLAGS += -g -O0 -fno-inline
 debug: fasttext
 
@@ -52,4 +55,4 @@ fasttext: $(OBJS) src/fasttext.cc
 	$(CXX) $(CXXFLAGS) $(OBJS) src/main.cc -o fasttext
 
 clean:
-	rm -rf *.o fasttext
+	rm -rf *.o *.gcno *.gcda fasttext

+ 17 - 1
setup.py

@@ -37,6 +37,13 @@ class get_pybind_include(object):
         import pybind11
         return pybind11.get_include(self.user)
 
+try:
+    coverage_index = sys.argv.index('--coverage')
+except ValueError:
+    coverage = False
+else:
+    del sys.argv[coverage_index]
+    coverage = True
 
 fasttext_src_files = map(str, os.listdir(FASTTEXT_SRC))
 fasttext_src_cc = list(filter(lambda x: x.endswith('.cc'), fasttext_src_files))
@@ -59,7 +66,8 @@ ext_modules = [
             FASTTEXT_SRC,
         ],
         language='c++',
-        extra_compile_args=["-O3 -funroll-loops -pthread -march=native"],
+        extra_compile_args=["-O0 -fno-inline -fprofile-arcs -pthread -march=native" if coverage else
+                            "-O3 -funroll-loops -pthread -march=native"],
     ),
 ]
 
@@ -115,6 +123,13 @@ class BuildExt(build_ext):
                 )
         ct = self.compiler.compiler_type
         opts = self.c_opts.get(ct, [])
+        extra_link_args = []
+
+        if coverage:
+            coverage_option = '--coverage'
+            opts.append(coverage_option)
+            extra_link_args.append(coverage_option)
+
         if ct == 'unix':
             opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
             opts.append(cpp_flag(self.compiler))
@@ -126,6 +141,7 @@ class BuildExt(build_ext):
             )
         for ext in self.extensions:
             ext.extra_compile_args = opts
+            ext.extra_link_args = extra_link_args
         build_ext.build_extensions(self)