Переглянути джерело

Merge topic 'fix-bfd'

* fix-bfd:
  Add ctkBinaryFileDescriptorTest
  ctkBinaryFileDescriptor - Fix "old-style-cast" warnings
  Use bfd shared library to compile Libs/Core/ctkBinaryFileDescriptor
  Add missing include statement and fix import in ctkBinaryFileDescriptor.h
  Use HAVE_BFD instead of CTK_HAVE_BFD in Libs/Core/CMakeLists.txt
  Build an executable instead of a library as part of the HAVE_BFD test
Jean-Christophe Fillion-Robin 14 роки тому
батько
коміт
e13114dd89

+ 6 - 6
Libs/Core/CMake/TestBFD/CMakeLists.txt

@@ -1,8 +1,8 @@
+CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
+
 PROJECT(TestBFD)
 
-IF(BUILD_SHARED_LIBS)
-  ADD_LIBRARY(TestBFD SHARED TestBFD.cpp)
-ELSE(BUILD_SHARED_LIBS)
-  ADD_LIBRARY(TestBFD TestBFD.cpp)
-ENDIF(BUILD_SHARED_LIBS)
-TARGET_LINK_LIBRARIES(TestBFD bfd iberty)
+FIND_LIBRARY(BDF_LIBRARY bfd)
+
+ADD_EXECUTABLE(TestBFD TestBFD.cpp)
+TARGET_LINK_LIBRARIES(TestBFD ${BDF_LIBRARY})

+ 7 - 1
Libs/Core/CMake/TestBFD/TestBFD.cpp

@@ -1,10 +1,16 @@
+
 #include <bfd.h>
 
-void TestBFD()
+// STD includes
+#include <cstdlib>
+
+int main(int /*argc*/, char * /*argv*/[])
 {
   bfd *abfd = 0;
   asymbol *symbol = 0;
   asection *p = 0;
 
   bfd_init();
+  
+  return EXIT_SUCCESS;
 }

+ 5 - 3
Libs/Core/CMake/ctkMacroBFDCheck.cmake

@@ -10,7 +10,9 @@ IF(NOT WIN32)
   INCLUDE(CheckIncludeFile)
   CHECK_INCLUDE_FILE(bfd.h HAVE_BFD_HEADER)
 
-  IF(HAVE_BFD_HEADER)
+  FIND_LIBRARY(BDF_LIBRARY bfd)
+
+  IF(HAVE_BFD_HEADER AND BDF_LIBRARY)
     # make sure we can build with libbfd
     #MESSAGE(STATUS "Checking libbfd")
     TRY_COMPILE(HAVE_BFD
@@ -24,8 +26,8 @@ IF(NOT WIN32)
     #MESSAGE(${OUTPUT})
 
     IF(HAVE_BFD)
-      SET(BFD_LIBRARIES bfd iberty)
-      MESSAGE(STATUS "CTKCore: libbfd is available")
+      SET(BFD_LIBRARIES ${BDF_LIBRARY})
+      MESSAGE(STATUS "CTKCore: libbfd is available [${BDF_LIBRARY}]")
     ELSE()
       MESSAGE(STATUS "CTKCore: libbfd is *NOT* available")
     ENDIF()

+ 2 - 2
Libs/Core/CMakeLists.txt

@@ -4,7 +4,7 @@ PROJECT(CTKCore)
 SET(CMAKE_MODULE_PATH ${CTKCore_SOURCE_DIR}/CMake ${CMAKE_MODULE_PATH})
 
 # CMake Macros
-INCLUDE(CMake/ctkMacroBFDCheck.cmake)
+INCLUDE(CMake/ctkMacroBFDCheck.cmake) # HAVE_BFD will be set to True if it applies
 
 #
 # See CTK/CMake/ctkMacroBuildLib.cmake for details
@@ -53,7 +53,7 @@ SET(KIT_SRCS
   ctkWorkflowTransitions.h
   )
 
-IF(CTK_HAVE_BFD)
+IF(HAVE_BFD)
   LIST(APPEND KIT_SRCS
     ctkBinaryFileDescriptor.cpp
     ctkBinaryFileDescriptor.h

+ 36 - 9
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -1,4 +1,4 @@
-# Dummy plugin
+# Dummy plugin used by ctkAbstractPluginFactoryTest1
 ADD_DEFINITIONS( -DCTKDummyPlugin)
 ctkMacroBuildLib(
   NAME "CTKDummyPlugin"
@@ -12,10 +12,14 @@ ctkMacroBuildLib(
 GET_TARGET_PROPERTY(ctkDummyPluginPATH "CTKDummyPlugin" LOCATION)
 REMOVE_DEFINITIONS(-DCTKDummyPlugin)
 
-#normal test
+
+
 SET(KIT ${PROJECT_NAME})
 
-CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
+#
+# Test sources
+#
+SET(KITTests_SRCS
   ctkAbstractFactoryTest1.cpp
   ctkAbstractLibraryFactoryTest1.cpp
   ctkAbstractObjectFactoryTest1.cpp
@@ -32,6 +36,16 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
   ctkWorkflowTest1.cpp
   ctkWorkflowTest2.cpp
   ctkWorkflowTest3.cpp
+  )
+
+IF(HAVE_BFD)
+  LIST(APPEND KITTests_SRCS
+    ctkBinaryFileDescriptorTest1.cpp
+    )
+ENDIF()
+
+CREATE_TEST_SOURCELIST(Tests ${KIT}CppTests.cpp
+  ${KITTests_SRCS}
   #EXTRA_INCLUDE TestingMacros.h
   )
 
@@ -40,7 +54,10 @@ REMOVE (TestsToRun ${KIT}CppTests.cpp)
 
 SET(LIBRARY_NAME ${PROJECT_NAME})
 
-SET(Tests_SRCS
+#
+# Tests Helpers sources
+#
+SET(Tests_Helpers_SRCS
   ctkBranchingWorkflowStep.h
   ctkExampleDerivedWorkflowStep.cpp
   ctkExampleDerivedWorkflowStep.h
@@ -50,15 +67,21 @@ SET(Tests_SRCS
   ctkSingletonTestHelper.h
   )
 
-SET(Tests_MOC_SRCS
+SET(Tests_Helpers_MOC_SRCS
   ctkExampleWorkflowStepUsingSignalsAndSlots.h
   )
 
-SET(Tests_MOC_CPP)
-QT4_WRAP_CPP(Tests_MOC_CPP ${Tests_MOC_SRCS})
+SET(Tests_Helpers_MOC_CPP)
+QT4_WRAP_CPP(Tests_Helpers_MOC_CPP ${Tests_Helpers_MOC_SRCS})
 
-# Test library
-ADD_EXECUTABLE(${KIT}CppTests ${Tests} ${Tests_SRCS} ${Tests_MOC_CPP})
+IF(HAVE_BFD)
+  ADD_EXECUTABLE(ctkBinaryFileDescriptorTestHelper ctkBinaryFileDescriptorTestHelper.cpp)
+ENDIF()
+
+#
+# Test executable
+#
+ADD_EXECUTABLE(${KIT}CppTests ${Tests} ${Tests_Helpers_SRCS} ${Tests_Helpers_MOC_CPP})
 TARGET_LINK_LIBRARIES(${KIT}CppTests ${LIBRARY_NAME} ${CTK_BASE_LIBRARIES} CTKDummyPlugin)
 
 SET( KIT_TESTS ${CPP_TEST_PATH}/${KIT}CppTests)
@@ -78,6 +101,10 @@ SIMPLE_TEST( ctkAbstractObjectFactoryTest1 )
 ADD_TEST( ctkAbstractPluginFactoryTest1 ${KIT_TESTS} ctkAbstractPluginFactoryTest1 ${ctkDummyPluginPATH})
 SET_PROPERTY(TEST ctkAbstractPluginFactoryTest1 PROPERTY LABELS ${PROJECT_NAME})
 SIMPLE_TEST( ctkAbstractQObjectFactoryTest1 )
+IF(HAVE_BFD)
+  ADD_TEST(NAME ctkBinaryFileDescriptorTest1 COMMAND ${KIT_TESTS} ctkBinaryFileDescriptorTest1 $<TARGET_FILE:ctkBinaryFileDescriptorTestHelper>)
+  SET_PROPERTY(TEST ctkBinaryFileDescriptorTest1 PROPERTY LABELS ${PROJECT_NAME})
+ENDIF()
 SIMPLE_TEST( ctkCommandLineParserTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest1 )
 SIMPLE_TEST( ctkDependencyGraphTest2 )

+ 120 - 0
Libs/Core/Testing/Cpp/ctkBinaryFileDescriptorTest1.cpp

@@ -0,0 +1,120 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QFile>
+
+// CTK includes
+#include "ctkBinaryFileDescriptor.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkBinaryFileDescriptorTest1(int argc, char * argv[])
+{
+  if (argc <= 1)
+    {
+    std::cerr << "Missing argument" << std::endl;
+    return EXIT_FAILURE;
+    }
+  QString filePath(argv[1]);
+
+  if (!QFile::exists(filePath))
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "filePath [" << qPrintable(filePath)
+              << "] do *NOT* exist" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  ctkBinaryFileDescriptor bfd;
+  if (bfd.load())
+    {
+    // Should fail to load without any valid filename
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with load() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (!bfd.unload())
+    {
+    // Unload inconditionnally return True
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with unload() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (bfd.isLoaded())
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with isLoaded() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (!bfd.fileName().isEmpty())
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with fileName() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  bfd.setFileName(filePath);
+
+  if (bfd.fileName() != filePath)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with fileName() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (!bfd.load())
+    {
+    // Should succeed since a valid filename is provided
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with load() method" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  void * main_pointer = bfd.resolve("main");
+  if (!main_pointer)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Problem with resolve() method - "
+              << "Failed to revolve 'main' symbol !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  void * mtBlancElevationInMeters_pointer = bfd.resolve("MtBlancElevationInMeters");
+  int * mtBlancElevationInMeters = reinterpret_cast<int*>(mtBlancElevationInMeters_pointer);
+  if (!mtBlancElevationInMeters)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Failed to case 'mtBlancElevationInMeters' pointer !" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (*mtBlancElevationInMeters != 4810)
+    {
+    std::cerr << "Line " << __LINE__ << " - "
+              << "Failed to invoke function associated with symbol 'MtBlancElevationInMeters' !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}
+

+ 22 - 0
Libs/Core/Testing/Cpp/ctkBinaryFileDescriptorTestHelper.cpp

@@ -0,0 +1,22 @@
+
+// STD includes
+#include <iostream>
+#include <cstdlib>
+
+#ifdef WIN32
+# define BFD_TEST_HELPER_EXPORT __declspec(dllexport)
+#else
+# define BFD_TEST_HELPER_EXPORT
+#endif
+
+extern "C" {
+
+  BFD_TEST_HELPER_EXPORT int MtBlancElevationInMeters = 4810;
+}
+
+int main(int /*argc*/, char* /*argv*/[])
+{
+  std::cout << "Mt Blanc elevation is " << MtBlancElevationInMeters
+            << " meters" << std::endl;
+  return EXIT_SUCCESS;
+}

+ 4 - 3
Libs/Core/ctkBinaryFileDescriptor.cpp

@@ -81,7 +81,7 @@ void* ctkBinaryFileDescriptorPrivate::resolve(const char * symbol)
   
   // Get the symbol table
   long storageNeeded = bfd_get_symtab_upper_bound(this->BFD);
-  asymbol ** symbolTable = (asymbol **) malloc(storageNeeded);
+  asymbol ** symbolTable = reinterpret_cast<asymbol **>(malloc(storageNeeded));
   
   long numberOfSymbols = bfd_canonicalize_symtab(this->BFD, symbolTable);
     
@@ -109,7 +109,7 @@ void* ctkBinaryFileDescriptorPrivate::resolve(const char * symbol)
         // Get the contents of the section
         bfd_size_type sz = bfd_get_section_size (p);
         mem = malloc (sz);
-        if (bfd_get_section_contents(this->BFD, p, mem, (file_ptr) 0,sz))
+        if (bfd_get_section_contents(this->BFD, p, mem, static_cast<file_ptr>(0), sz))
           {
           this->Sections.push_back( MemorySectionType(p, mem) );
           }
@@ -127,7 +127,8 @@ void* ctkBinaryFileDescriptorPrivate::resolve(const char * symbol)
         }
             
       // determine the address of this section
-      addr = (char *)mem + (bfd_asymbol_value(symbolTable[i]) - bfd_asymbol_base(symbolTable[i]));
+      addr = reinterpret_cast<char *>(mem)
+          + (bfd_asymbol_value(symbolTable[i]) - bfd_asymbol_base(symbolTable[i]));
       break;
       }
     }

+ 3 - 2
Libs/Core/ctkBinaryFileDescriptor.h

@@ -34,8 +34,9 @@
 
 // Qt includes
 #include <QString>
+#include <QScopedPointer>
 
-#include "ctkWidgetsExport.h"
+#include "ctkCoreExport.h"
 
 
 class ctkBinaryFileDescriptorPrivate;
@@ -43,7 +44,7 @@ class ctkBinaryFileDescriptorPrivate;
 /// \ingroup Core
 /// Allows to resolve global symbols contained into an executable.
 /// Implementation valid only for unix-like systems (Linux, Mac, ...)
-class CTK_WIDGETS_EXPORT ctkBinaryFileDescriptor
+class CTK_CORE_EXPORT ctkBinaryFileDescriptor
 {
 public:
   ctkBinaryFileDescriptor();