Browse Source

Tutorial: Improve Step 9

* Move USE_MYMATH from configured header to target_compile_definitions
Betsy McPhail 6 years ago
parent
commit
6f6a32a0f6

+ 3 - 3
Help/guide/tutorial/Complete/MathFunctions/CMakeLists.txt

@@ -13,6 +13,8 @@ target_include_directories(MathFunctions
 option(USE_MYMATH "Use tutorial provided math implementation" ON)
 if(USE_MYMATH)
 
+  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
+
   # first we add the executable that generates the table
   add_executable(MakeTable MakeTable.cxx)
   target_link_libraries(MakeTable tutorial_compiler_flags)
@@ -35,14 +37,12 @@ if(USE_MYMATH)
                              ${CMAKE_CURRENT_BINARY_DIR}
                              )
 
+  # state that SqrtLibrary need PIC when the default is shared libraries
   set_target_properties(SqrtLibrary PROPERTIES
                         POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
                         )
 
   target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
-
-
-  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
   target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
 endif()
 

+ 3 - 2
Help/guide/tutorial/MultiPackage/MathFunctions/CMakeLists.txt

@@ -13,6 +13,8 @@ target_include_directories(MathFunctions
 option(USE_MYMATH "Use tutorial provided math implementation" ON)
 if(USE_MYMATH)
 
+  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
+
   # first we add the executable that generates the table
   add_executable(MakeTable MakeTable.cxx)
 
@@ -34,6 +36,7 @@ if(USE_MYMATH)
                              ${CMAKE_CURRENT_BINARY_DIR}
                              )
 
+  # state that SqrtLibrary need PIC when the default is shared libraries
   set_target_properties(SqrtLibrary PROPERTIES
                         POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
                         )
@@ -41,8 +44,6 @@ if(USE_MYMATH)
   target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
 endif()
 
-target_compile_definitions(MathFunctions PRIVATE "$<$<BOOL:${USE_MYMATH}>:USE_MYMATH>")
-
 # define the symbol stating we are using the declspec(dllexport) when
 # building on windows
 target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")

+ 0 - 1
Help/guide/tutorial/MultiPackage/tutorial.cxx

@@ -1,6 +1,5 @@
 // A simple program that computes the square root of a number
 #include <iostream>
-#include <sstream>
 #include <string>
 
 #include "MathFunctions.h"

+ 2 - 2
Help/guide/tutorial/Step10/MathFunctions/CMakeLists.txt

@@ -11,6 +11,8 @@ target_include_directories(MathFunctions
 option(USE_MYMATH "Use tutorial provided math implementation" ON)
 if(USE_MYMATH)
 
+  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
+
   # first we add the executable that generates the table
   add_executable(MakeTable MakeTable.cxx)
 
@@ -37,8 +39,6 @@ if(USE_MYMATH)
                         POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
                         )
 
-  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
-
   target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
 endif()
 

+ 3 - 2
Help/guide/tutorial/Step11/MathFunctions/CMakeLists.txt

@@ -11,6 +11,8 @@ target_include_directories(MathFunctions
 option(USE_MYMATH "Use tutorial provided math implementation" ON)
 if(USE_MYMATH)
 
+  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
+
   # first we add the executable that generates the table
   add_executable(MakeTable MakeTable.cxx)
   target_link_libraries(MakeTable tutorial_compiler_flags)
@@ -33,13 +35,12 @@ if(USE_MYMATH)
                              ${CMAKE_CURRENT_BINARY_DIR}
                              )
 
+  # state that SqrtLibrary need PIC when the default is shared libraries
   set_target_properties(SqrtLibrary PROPERTIES
                         POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
                         )
 
   target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
-
-  target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
   target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
 endif()
 

+ 0 - 1
Help/guide/tutorial/Step11/tutorial.cxx

@@ -1,5 +1,4 @@
 // A simple program that computes the square root of a number
-#include <cmath>
 #include <iostream>
 #include <string>
 

+ 2 - 1
Help/guide/tutorial/Step9/CMakeLists.txt

@@ -10,7 +10,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
 # should we use our own math functions
 option(USE_MYMATH "Use tutorial provided math implementation" ON)
 
-# configure a header file to pass the version number only
+# configure a header file to pass some of the CMake settings
+# to the source code
 configure_file(TutorialConfig.h.in TutorialConfig.h)
 
 # add the MathFunctions library

+ 0 - 4
Help/guide/tutorial/Step9/MathFunctions/CMakeLists.txt

@@ -22,10 +22,6 @@ target_include_directories(MathFunctions
           PRIVATE   ${CMAKE_CURRENT_BINARY_DIR}
           )
 
-# use compile definitions to state if we have enabled USE_MYMATH
-# and that anything that links to use will get this define
-target_compile_definitions(MathFunctions INTERFACE "USE_MYMATH")
-
 # install rules
 install(TARGETS MathFunctions DESTINATION lib)
 install(FILES MathFunctions.h DESTINATION include)

+ 2 - 1
Help/guide/tutorial/Step9/TutorialConfig.h.in

@@ -1,3 +1,4 @@
-// the configured version number
+// the configured options and settings for Tutorial
 #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
 #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
+#cmakedefine USE_MYMATH

+ 0 - 1
Help/guide/tutorial/Step9/tutorial.cxx

@@ -1,7 +1,6 @@
 // A simple program that computes the square root of a number
 #include <cmath>
 #include <iostream>
-#include <sstream>
 #include <string>
 
 #include "TutorialConfig.h"

+ 7 - 2
Help/guide/tutorial/index.rst

@@ -645,7 +645,6 @@ The first step is to update the starting section of the top-level
 
 .. literalinclude:: Step10/CMakeLists.txt
   :language: cmake
-  :start-after: set(CMAKE_CXX_STANDARD 14)
   :end-before: # add the binary tree
 
 Now that we have made MathFunctions always be used, we will need to update
@@ -658,7 +657,7 @@ The end result is that ``MathFunctions/CMakeLists.txt`` should look like:
 
 .. literalinclude:: Step10/MathFunctions/CMakeLists.txt
   :language: cmake
-  :lines: 1-40,46-
+  :lines: 1-36,42-
 
 Next, update ``MathFunctions/mysqrt.cxx`` to use the ``mathfunctions`` and
 ``detail`` namespaces:
@@ -671,6 +670,7 @@ uses ``USE_MYMATH``:
 
 #. Always include ``MathFunctions.h``
 #. Always use ``mathfunctions::sqrt``
+#. Don't include cmath
 
 Finally, update ``MathFunctions/MathFunctions.h`` to use dll export defines:
 
@@ -683,9 +683,14 @@ library that has position enabled code. The solution to this is to explicitly
 set the ``POSITION_INDEPENDENT_CODE`` target property of SqrtLibrary to be
 True no matter the build type.
 
+.. literalinclude:: Step10/MathFunctions/CMakeLists.txt
+  :language: cmake
+  :lines: 37-42
+
 **Exercise**: We modified ``MathFunctions.h`` to use dll export defines.
 Using CMake documentation can you find a helper module to simplify this?
 
+
 Adding Generator Expressions (Step 10)
 ======================================