Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 10560)
+++ CMakeLists.txt	(working copy)
@@ -63,6 +63,77 @@
 SET(CMAKE_MODULE_PATH "${OpenSceneGraph_SOURCE_DIR}/CMakeModules;${CMAKE_MODULE_PATH}")
 
 
+################################################################################
+# Installation stuff
+
+SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on windows")
+ADD_DEFINITIONS(-DOSG_DEBUG_POSTFIX=${CMAKE_DEBUG_POSTFIX})
+
+IF(UNIX AND NOT WIN32)
+  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
+  SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
+ENDIF()
+
+IF(CYGWIN)
+  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
+  SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
+ENDIF()
+
+IF(UNIX AND NOT WIN32 AND NOT APPLE)
+  IF(CMAKE_SIZEOF_VOID_P MATCHES "8")
+      SET(LIB_POSTFIX "64" CACHE STRING "suffix for 32/64 dir placement")
+      MARK_AS_ADVANCED(LIB_POSTFIX)
+  ENDIF()
+ENDIF()
+IF(NOT DEFINED LIB_POSTFIX)
+    SET(LIB_POSTFIX "")
+ENDIF()
+
+# Here we apparantly do some funky stuff with making the bin/ and lib/
+# folders which is probably needed to work around a very old CMake bug?
+
+#SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin/${CMAKE_SYSTEM_NAME})
+SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin)
+MAKE_DIRECTORY(${OUTPUT_BINDIR})
+IF(MSVC AND NOT MSVC_IDE)
+    MAKE_DIRECTORY(${OUTPUT_BINDIR}/${OSG_PLUGINS})
+ENDIF(MSVC AND NOT MSVC_IDE)
+
+#SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib/${CMAKE_SYSTEM_NAME})
+SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib)
+MAKE_DIRECTORY(${OUTPUT_LIBDIR})
+IF(NOT MSVC)
+    MAKE_DIRECTORY(${OUTPUT_LIBDIR}/${OSG_PLUGINS})
+ENDIF(NOT MSVC)
+
+# On CMake 2.4.x use EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH and later
+# we work around the DLL placement by use of the PREFIX target property hack
+#
+# On CMake 2.6.x use the newly minted CMAKE_LIBRARY_OUTPUT_DIRECTORY,
+# CMAKE_ARCHIVE_OUTPUT_DIRECTORY & CMAKE_RUNTIME_OUTPUT_DIRECTORY
+
+IF(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 5)
+    SET(EXECUTABLE_OUTPUT_PATH ${OUTPUT_BINDIR})
+    SET(LIBRARY_OUTPUT_PATH    ${OUTPUT_LIBDIR})
+ELSE()
+    # If CMake >= 2.6.0
+    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR})
+    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_BINDIR})
+    IF(WIN32)
+        SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_BINDIR})
+    ELSE(WIN32)
+        SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR})
+    ENDIF(WIN32)
+ENDIF()
+
+#SET(INSTALL_BINDIR OpenSceneGraph/bin)
+#SET(INSTALL_INCDIR OpenSceneGraph/include)
+#SET(INSTALL_LIBDIR OpenSceneGraph/lib)
+#SET(INSTALL_DOCDIR OpenSceneGraph/doc)
+
+################################################################################
+# Library finding stuff.
+
 # Okay, here's the problem: On some platforms, linking against OpenThreads
 # is not enough and explicit linking to the underlying thread library
 # is also required (e.g. FreeBSD). But OpenThreads may be built with different
@@ -308,6 +379,16 @@
 OPTION(BUILD_OSG_EXAMPLES "Enable to build OSG Examples" OFF)
 
 ################################################################################
+
+
+################################################################################
+# Optionally, use CMakePorts to satisfy certain dependencies
+
+OPTION(BUILD_CMAKEPORTS "Use CMakePorts to compile local versions of some dependencies" OFF)
+IF(BUILD_CMAKEPORTS)
+    INCLUDE(CMakePortsSearch)
+ENDIF()
+
 # 3rd Party Dependency Stuff
 IF(WIN32)
     INCLUDE(Find3rdPartyDependencies)
@@ -469,74 +550,6 @@
 
 
 ################################################################################
-# Installation stuff
-
-SET(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "add a postfix, usually d on windows")
-ADD_DEFINITIONS(-DOSG_DEBUG_POSTFIX=${CMAKE_DEBUG_POSTFIX})
-
-IF(UNIX AND NOT WIN32)
-  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
-  SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
-ENDIF()
-
-IF(CYGWIN)
-  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -D_DEBUG")
-  SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
-ENDIF()
-
-IF(UNIX AND NOT WIN32 AND NOT APPLE)
-  IF(CMAKE_SIZEOF_VOID_P MATCHES "8")
-      SET(LIB_POSTFIX "64" CACHE STRING "suffix for 32/64 dir placement")
-      MARK_AS_ADVANCED(LIB_POSTFIX)
-  ENDIF()
-ENDIF()
-IF(NOT DEFINED LIB_POSTFIX)
-    SET(LIB_POSTFIX "")
-ENDIF()
-
-# Here we apparantly do some funky stuff with making the bin/ and lib/
-# folders which is probably needed to work around a very old CMake bug?
-
-#SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin/${CMAKE_SYSTEM_NAME})
-SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/bin)
-MAKE_DIRECTORY(${OUTPUT_BINDIR})
-IF(MSVC AND NOT MSVC_IDE)
-    MAKE_DIRECTORY(${OUTPUT_BINDIR}/${OSG_PLUGINS})
-ENDIF(MSVC AND NOT MSVC_IDE)
-
-#SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib/${CMAKE_SYSTEM_NAME})
-SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/lib)
-MAKE_DIRECTORY(${OUTPUT_LIBDIR})
-IF(NOT MSVC)
-    MAKE_DIRECTORY(${OUTPUT_LIBDIR}/${OSG_PLUGINS})
-ENDIF(NOT MSVC)
-
-# On CMake 2.4.x use EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH and later
-# we work around the DLL placement by use of the PREFIX target property hack
-#
-# On CMake 2.6.x use the newly minted CMAKE_LIBRARY_OUTPUT_DIRECTORY,
-# CMAKE_ARCHIVE_OUTPUT_DIRECTORY & CMAKE_RUNTIME_OUTPUT_DIRECTORY
-
-IF(CMAKE_MAJOR_VERSION EQUAL 2 AND CMAKE_MINOR_VERSION LESS 5)
-    SET(EXECUTABLE_OUTPUT_PATH ${OUTPUT_BINDIR})
-    SET(LIBRARY_OUTPUT_PATH    ${OUTPUT_LIBDIR})
-ELSE()
-    # If CMake >= 2.6.0
-    SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR})
-    SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_BINDIR})
-    IF(WIN32)
-        SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_BINDIR})
-    ELSE(WIN32)
-        SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR})
-    ENDIF(WIN32)
-ENDIF()
-
-#SET(INSTALL_BINDIR OpenSceneGraph/bin)
-#SET(INSTALL_INCDIR OpenSceneGraph/include)
-#SET(INSTALL_LIBDIR OpenSceneGraph/lib)
-#SET(INSTALL_DOCDIR OpenSceneGraph/doc)
-
-################################################################################
 # User Options
 
 
Index: CMakeModules/CMakePortsSearch.cmake
===================================================================
--- CMakeModules/CMakePortsSearch.cmake	(revision 0)
+++ CMakeModules/CMakePortsSearch.cmake	(revision 0)
@@ -0,0 +1,79 @@
+# Internal function to enable the PREFIX hack so DLLs
+# get generated in same directory as OSG binaries under VS
+function(_CMPS_HACK_PREFIX_IF_NEEDED)
+    foreach(_target ${ARGN})
+        # IFF the target exists and we are using VS use the PREFIX hack
+        if(TARGET ${_target})
+            if(MSVC_IDE AND OSG_MSVC_VERSIONED_DLL)
+                set_target_properties(${_target} PROPERTIES PREFIX "../")
+            endif()
+        endif()
+    endforeach()
+endfunction()
+
+# Internal function to exclude specified targets from the default
+# build target (for unneeded test programs and the like built as
+# part of certain CMakePorts)
+function(_CMPS_EXCLUDE_FROM_ALL)
+    foreach(_target ${ARGN})
+        # IFF the target exists exclude it from default build
+        if(TARGET ${_target})
+            set_target_properties(${_target} PROPERTIES EXCLUDE_FROM_ALL true)
+        endif()
+    endforeach()
+endfunction()
+
+# CMakePorts relies on the standard BUILD_SHARED_LIBS
+# CMake option
+set(_SAVE_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
+if(DYNAMIC_OPENSCENEGRAPH)
+    set(BUILD_SHARED_LIBS TRUE)
+else()
+    set(BUILD_SHARED_LIBS FALSE)
+endif()
+
+# These options allow us to control what a CMakePort make installs
+# in this case all we care about are libraries so skip other stuff
+set(SKIP_INSTALL_FILES TRUE)
+set(SKIP_INSTALL_HEADERS TRUE)
+set(SKIP_INSTALL_EXECUTABLES TRUE)
+
+# Brand the libraries with our own prefix to prevent overlap on a
+# make install to somewhere like /usr/local
+set(CMAKEPORTS_NAME_PREFIX "osg_")
+
+set(CMAKEPORTS_DIR "" CACHE PATH "Path to the directory where the CMakePorts are unzipped")
+
+if(CMAKEPORTS_DIR)
+    # Attempt to build copies of libjpeg, zlib, and libpng provided they
+    # are there
+    if(EXISTS ${CMAKEPORTS_DIR}/jpeg)
+        add_subdirectory(${CMAKEPORTS_DIR}/jpeg ${CMAKE_CURRENT_BINARY_DIR}/cmakeports_jpeg)
+        _CMPS_HACK_PREFIX_IF_NEEDED(jpeg cjpeg djpeg jpegtran rdjpgcom wrjpgcom)
+        _CMPS_EXCLUDE_FROM_ALL(cjpeg djpeg jpegtran rdjpgcom wrjpgcom)
+    endif()
+    if(EXISTS ${CMAKEPORTS_DIR}/zlib)
+        add_subdirectory(${CMAKEPORTS_DIR}/zlib ${CMAKE_CURRENT_BINARY_DIR}/cmakeports_zlib)
+        _CMPS_HACK_PREFIX_IF_NEEDED(zlib)
+    endif()
+    if(EXISTS ${CMAKEPORTS_DIR}/png)
+        add_subdirectory(${CMAKEPORTS_DIR}/png  ${CMAKE_CURRENT_BINARY_DIR}/cmakeports_png)
+        _CMPS_HACK_PREFIX_IF_NEEDED(png12 pngtest)
+        _CMPS_EXCLUDE_FROM_ALL(pngtest)
+    endif()
+endif()
+
+MESSAGE(STATUS "Detected these CMakePorts to be built alongside OSG:")
+if(JPEG_FOUND AND TARGET jpeg)
+    MESSAGE(STATUS "   jpeg")
+endif()
+if(ZLIB_FOUND AND TARGET zlib)
+    MESSAGE(STATUS "   zlib")
+endif()
+if(PNG_FOUND)
+   if(TARGET png12 OR TARGET png12_static)
+       MESSAGE(STATUS "   png")
+   endif()
+endif()
+
+set(BUILD_SHARED_LIBS ${_SAVE_BUILD_SHARED_LIBS})
