Merge branch 'mingw-port'
authorAntonio Ospite <ospite@studenti.unina.it>
Thu, 10 May 2012 14:02:16 +0000 (16:02 +0200)
committerAntonio Ospite <ospite@studenti.unina.it>
Thu, 10 May 2012 14:02:16 +0000 (16:02 +0200)
CMakeLists.txt
HACKING.asciidoc
cmake_modules/FindFFmpeg.cmake
cmake_modules/mingw_cross_toolchain.cmake [new file with mode: 0644]
examples/CMakeLists.txt
examples/picoproj.c
src/CMakeLists.txt
src/am7xxx.c
src/serialize.c

index 23627fa..3e525cc 100644 (file)
@@ -68,21 +68,27 @@ if (CMAKE_COMPILER_IS_GNUCC)
     -Wwrite-strings
     -fstack-protector
     --param=ssp-buffer-size=4)
+endif()
+
+set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb -DDEBUG=1")
+set(CMAKE_C_FLAGS_RELEASE "-O2")
+set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
 
+if (CMAKE_COMPILER_IS_GNUCC)
   if (STRICT_COMPILATION_CHECKS)
-    add_flags(CMAKE_C_FLAGS
+    add_flags(STRICT_FLAGS
       -Werror
       # NOTE: Vanilla libusb-1.0.8 can't live with -pedantic-errors
       -pedantic-errors
       # NOTE: GCC >= 4.6 is needed for -Wunused-but-set-variable
       -Wunused-but-set-variable)
+
+    add_flags(CMAKE_C_FLAGS_DEBUG ${STRICT_FLAGS})
+    add_flags(CMAKE_C_FLAGS_RELEASE ${STRICT_FLAGS})
+    add_flags(CMAKE_C_FLAGS_RELWITHDEBINFO ${STRICT_FLAGS})
   endif()
 endif()
 
-set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb -DDEBUG=1")
-set(CMAKE_C_FLAGS_RELEASE "-O2")
-set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g")
-
 # Add library project
 add_subdirectory(src)
 add_subdirectory(examples)
index 99dc15e..ed2a46b 100644 (file)
@@ -11,7 +11,7 @@ The suggested way to hack on the project is:
 
   $ mkdir build
   $ cd build
-  $ cmake ../ -DCMAKE_BUILD_TYPE=debug -DSTRICT_COMPILATION_CHECKS=ON
+  $ cmake -D CMAKE_BUILD_TYPE=debug -D STRICT_COMPILATION_CHECKS=ON ../
   $ make
 
 If you want to check the code with the ''sparse'' static analysis tool you
@@ -19,9 +19,31 @@ can run:
 
   $ mkdir build
   $ cd build
-  $ cmake ../ -DCMAKE_C_COMPILER=cgcc
+  $ cmake -D CMAKE_C_COMPILER=cgcc ../
   $ make
 
+=== Cross Builds
+
+If you want to build for MS Windows:
+
+  $ sudo aptitude install mingw-w64
+  $ mkdir build
+  $ cd build
+  $ wget -nv http://sourceforge.net/projects/libusbx/files/releases/1.0.11/Windows/libusbx-1.0.11-win.7z
+  $ 7z -olibusbx-1.0.11-win x libusbx-1.0.11-win.7z
+  $ cmake  \
+          -D GNU_HOST=i686-w64-mingw32 \
+          -D CMAKE_TOOLCHAIN_FILE=../cmake_modules/mingw_cross_toolchain.cmake \
+          -D CMAKE_INSTALL_PREFIX=libam7xxx-win/ \
+          -D LIBUSB_1_INCLUDE_DIR=libusbx-1.0.11-win/include/libusbx-1.0 \
+          -D LIBUSB_1_LIBRARY=libusbx-1.0.11-win/MinGW32/dll/libusb-1.0.dll \
+          -D BUILD_AM7XXX-PLAY:BOOL=FALSE \
+          ../
+  $ make
+
+After that you will find libam7xxx.dll in lib/ and picoproj.exe in the bin/
+directory.
+
 === Valgrind
 
 You can run the test program under the http://valgrind.org/[valgrind]
index 1dc76d5..c88d694 100644 (file)
@@ -27,11 +27,12 @@ MACRO(FFMPEG_FIND varname shortname headername)
     # old version of ffmpeg put header in $prefix/include/[ffmpeg]
     # so try to find header in include directory
     FIND_PATH(FFMPEG_${varname}_INCLUDE_DIRS ${headername}
-        PATHS
+        HINTS
         ${FFMPEG_ROOT}/include
         $ENV{FFMPEG_DIR}/include
         $ENV{OSGDIR}/include
         $ENV{OSG_ROOT}/include
+        PATHS
         ~/Library/Frameworks
         /Library/Frameworks
         /usr/local/include
@@ -49,10 +50,12 @@ MACRO(FFMPEG_FIND varname shortname headername)
     # so try to find lib${shortname}/header in include directory
     IF(NOT FFMPEG_${varname}_INCLUDE_DIRS)
         FIND_PATH(FFMPEG_${varname}_INCLUDE_DIRS lib${shortname}/${headername}
+            HINTS
             ${FFMPEG_ROOT}/include
             $ENV{FFMPEG_DIR}/include
             $ENV{OSGDIR}/include
             $ENV{OSG_ROOT}/include
+            PATHS
             ~/Library/Frameworks
             /Library/Frameworks
             /usr/local/include
@@ -69,11 +72,12 @@ MACRO(FFMPEG_FIND varname shortname headername)
 
     FIND_LIBRARY(FFMPEG_${varname}_LIBRARIES
         NAMES ${shortname}
-        PATHS
+        HINTS
         ${FFMPEG_ROOT}/lib
         $ENV{FFMPEG_DIR}/lib
         $ENV{OSGDIR}/lib
         $ENV{OSG_ROOT}/lib
+        PATHS
         ~/Library/Frameworks
         /Library/Frameworks
         /usr/local/lib
diff --git a/cmake_modules/mingw_cross_toolchain.cmake b/cmake_modules/mingw_cross_toolchain.cmake
new file mode 100644 (file)
index 0000000..32ef5ab
--- /dev/null
@@ -0,0 +1,9 @@
+SET(CMAKE_SYSTEM_NAME Windows)
+include(CMakeForceCompiler)
+IF("${GNU_HOST}" STREQUAL "")
+    SET(GNU_HOST i586-mingw32msvc)
+ENDIF()
+# Prefix detection only works with compiler id "GNU"
+CMAKE_FORCE_C_COMPILER(${GNU_HOST}-gcc GNU)
+# CMake doesn't automatically look for prefixed 'windres', do it manually:
+SET(CMAKE_RC_COMPILER ${GNU_HOST}-windres)
index 234e666..b7941ab 100644 (file)
@@ -1,3 +1,4 @@
+include(CheckSymbolExists)
 add_definitions("-D_POSIX_C_SOURCE=2") # for getopt()
 add_definitions("-D_POSIX_SOURCE") # for sigaction
 add_definitions("-D_BSD_SOURCE") # for strdup
@@ -5,31 +6,46 @@ add_definitions("-D_BSD_SOURCE") # for strdup
 include_directories(${CMAKE_SOURCE_DIR}/src/)
 
 # Build a test app that sends a single picture
-add_executable(picoproj picoproj.c)
-target_link_libraries(picoproj am7xxx)
-install(TARGETS picoproj
-  DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
+option(BUILD_PICOPROJ "Build a test app that sends a single picture" TRUE)
+if(BUILD_PICOPROJ)
+  add_executable(picoproj picoproj.c)
+  target_link_libraries(picoproj am7xxx)
+  install(TARGETS picoproj
+    DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
+endif()
 
 # Build a more complete example
-find_package(FFmpeg REQUIRED)
+option(BUILD_AM7XXX-PLAY "Build a more complete example: am7xxx-play" TRUE)
+if(BUILD_AM7XXX-PLAY)
+  find_package(FFmpeg REQUIRED)
+  set(CMAKE_REQUIRED_LIBRARIES ${FFMPEG_LIBRARIES}) 
+  set(CMAKE_REQUIRED_INCLUDES ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}) 
+  check_symbol_exists(avformat_open_input
+    "${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS}/libavformat/avformat.h"
+    HAVE_AVFORMAT_OPEN_INPUT)
+  if(NOT HAVE_AVFORMAT_OPEN_INPUT)
+    message(FATAL_ERROR
+      "Function avformat_open_input missing. Please use a newer FFmpeg release.")
+  endif()
 
-include_directories(${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS})
-include_directories(${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS})
-include_directories(${FFMPEG_LIBSWSCALE_INCLUDE_DIRS})
+  include_directories(${FFMPEG_LIBAVDEVICE_INCLUDE_DIRS})
+  include_directories(${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS})
+  include_directories(${FFMPEG_LIBSWSCALE_INCLUDE_DIRS})
 
-# xcb is used to retrieve the full screen dimensions when using x11grab
-# as input format
-find_package(XCB)
-if (XCB_FOUND)
-  add_definitions("${LIBXCB_DEFINITIONS} -DHAVE_XCB")
-  include_directories(${LIBXCB_INCLUDE_DIRS})
-endif()
+  # xcb is used to retrieve the full screen dimensions when using x11grab
+  # as input format
+  find_package(XCB)
+  if (XCB_FOUND)
+    add_definitions("${LIBXCB_DEFINITIONS} -DHAVE_XCB")
+    include_directories(${LIBXCB_INCLUDE_DIRS})
+  endif()
 
-add_executable(am7xxx-play am7xxx-play.c)
+  add_executable(am7xxx-play am7xxx-play.c)
 
-target_link_libraries(am7xxx-play am7xxx
-  ${FFMPEG_LIBRARIES}
-  ${FFMPEG_LIBSWSCALE_LIBRARIES}
-  ${LIBXCB_LIBRARIES})
-install(TARGETS am7xxx-play
-  DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
+  target_link_libraries(am7xxx-play am7xxx
+    ${FFMPEG_LIBRARIES}
+    ${FFMPEG_LIBSWSCALE_LIBRARIES}
+    ${LIBXCB_LIBRARIES})
+  install(TARGETS am7xxx-play
+    DESTINATION "${CMAKE_INSTALL_PREFIX}/bin")
+endif()
index 2c1b257..8503612 100644 (file)
@@ -24,7 +24,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -58,7 +57,7 @@ int main(int argc, char *argv[])
        int opt;
 
        char filename[FILENAME_MAX] = {0};
-       int image_fd;
+       FILE *image_fp;
        struct stat st;
        am7xxx_context *ctx;
        am7xxx_device *dev;
@@ -74,6 +73,8 @@ int main(int argc, char *argv[])
        while ((opt = getopt(argc, argv, "f:F:l:p:W:H:h")) != -1) {
                switch (opt) {
                case 'f':
+                       if (filename[0] != '\0')
+                               fprintf(stderr, "Warning: image file already specified\n");
                        strncpy(filename, optarg, FILENAME_MAX);
                        break;
                case 'F':
@@ -142,31 +143,43 @@ int main(int argc, char *argv[])
                goto out;
        }
 
-       image_fd = open(filename, O_RDONLY);
-       if (image_fd < 0) {
-               perror("open");
+       image_fp = fopen(filename, "rb");
+       if (image_fp == NULL) {
+               perror("fopen");
                exit_code = EXIT_FAILURE;
                goto out;
        }
-       if (fstat(image_fd, &st) < 0) {
+       if (fstat(fileno(image_fp), &st) < 0) {
                perror("fstat");
                exit_code = EXIT_FAILURE;
-               goto out_close_image_fd;
+               goto out_close_image_fp;
        }
        size = st.st_size;
 
-       image = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, image_fd, 0);
+       image = malloc(size * sizeof(unsigned char));
        if (image == NULL) {
-               perror("mmap");
+               perror("malloc");
                exit_code = EXIT_FAILURE;
-               goto out_close_image_fd;
+               goto out_close_image_fp;
+       }
+
+       ret = fread(image, size, 1, image_fp);
+       if (ret != 1) {
+               if (feof(image_fp))
+                       fprintf(stderr, "Unexpected end of file.\n");
+               else if (ferror(image_fp))
+                       perror("fread");
+               else
+                       fprintf(stderr, "Unexpected error condition.\n");
+
+               goto out_free_image;
        }
 
        ret = am7xxx_init(&ctx);
        if (ret < 0) {
                perror("am7xxx_init");
                exit_code = EXIT_FAILURE;
-               goto out_munmap;
+               goto out_free_image;
        }
 
        am7xxx_set_log_level(ctx, log_level);
@@ -195,7 +208,7 @@ int main(int argc, char *argv[])
 
        ret = am7xxx_get_device_info(dev, &device_info);
        if (ret < 0) {
-               perror("am7xxx_get_info");
+               perror("am7xxx_get_device_info");
                exit_code = EXIT_FAILURE;
                goto cleanup;
        }
@@ -225,15 +238,13 @@ int main(int argc, char *argv[])
 cleanup:
        am7xxx_shutdown(ctx);
 
-out_munmap:
-       ret = munmap(image, size);
-       if (ret < 0)
-               perror("munmap");
+out_free_image:
+       free(image);
 
-out_close_image_fd:
-       ret = close(image_fd);
-       if (ret < 0)
-               perror("close");
+out_close_image_fp:
+       ret = fclose(image_fp);
+       if (ret == EOF)
+               perror("fclose");
 
 out:
        exit(exit_code);
index e09a282..0817374 100644 (file)
@@ -22,7 +22,12 @@ endif()
 install(TARGETS am7xxx-static
    DESTINATION "${CMAKE_INSTALL_PREFIX}/lib")
 
-find_library(MATH_LIB m)
+if(NOT WIN32)
+  find_library(MATH_LIB m)
+else()
+  # not needed on windows
+  set(MATH_LIB "")
+endif()
 
 target_link_libraries(am7xxx ${MATH_LIB} ${LIBUSB_1_LIBRARIES})
 target_link_libraries(am7xxx-static ${MATH_LIB} ${LIBUSB_1_LIBRARIES})
index 8087f89..d22a417 100644 (file)
@@ -294,7 +294,7 @@ static int send_data(am7xxx_device *dev, uint8_t *buffer, unsigned int len)
 
        trace_dump_buffer(dev->ctx, "sending -->", buffer, len);
 
-       ret = libusb_bulk_transfer(dev->usb_device, 1, buffer, len, &transferred, 0);
+       ret = libusb_bulk_transfer(dev->usb_device, 0x1, buffer, len, &transferred, 0);
        if (ret != 0 || (unsigned int)transferred != len) {
                error(dev->ctx, "ret: %d\ttransferred: %d (expected %u)\n",
                      ret, transferred, len);
@@ -542,7 +542,7 @@ static int scan_devices(am7xxx_context *ctx, scan_op op,
                                                goto out;
                                        }
 
-                                       libusb_set_configuration((*dev)->usb_device, 1);
+                                       libusb_set_configuration((*dev)->usb_device, 2);
                                        libusb_claim_interface((*dev)->usb_device, 0);
                                        goto out;
                                }
index be300ea..72e3fd4 100644 (file)
  */
 
 #include <string.h>
+
+#ifdef __MINGW32__
+#define le32toh(x) (x)
+#define htole32(x) (x)
+#else
 #include <endian.h>
+#endif
 
 #include "serialize.h"