From 5ad09d0e13c80d3987eb5f27040384c39390a7d0 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Thu, 12 Dec 2013 16:52:29 +0000
Subject: [PATCH 1/4] CMakeLists.txt: Fixes and improvements

1. main.cpp doesn't exist anymore.
2. hlslang_tab.cpp is now Gen_hlslang_tab.cpp.
3. Remove redundancy in bison and flex execution.
4. Use find_package() to locate them if !windows.
---
 CMakeLists.txt | 137 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 68 insertions(+), 69 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f159a1d..dc30cd5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -59,7 +59,6 @@ set(MACHINE_INDEPENDENT_FILES
   hlslang/MachineIndependent/intermOut.cpp
   hlslang/MachineIndependent/IntermTraverse.cpp
   hlslang/MachineIndependent/localintermediate.h
-  #hlslang/MachineIndependent/parseConst.cpp
   hlslang/MachineIndependent/ParseHelper.cpp
   hlslang/MachineIndependent/ParseHelper.h
   hlslang/MachineIndependent/PoolAlloc.cpp
@@ -92,9 +91,9 @@ endif ()
 
 
 set(MACHINE_INDEPENDENT_GENERATED_SOURCE_FILES
-  ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent/Gen_hlslang.cpp
-  ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent/hlslang_tab.cpp
-  ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent/hlslang_tab.h
+  hlslang/MachineIndependent/Gen_hlslang.cpp
+  hlslang/MachineIndependent/Gen_hlslang_tab.cpp
+  hlslang/MachineIndependent/hlslang_tab.h
 )
 
 SET_SOURCE_FILES_PROPERTIES(${MACHINE_INDEPENDENT_GENERATED_SOURCE_FILES} PROPERTIES
@@ -107,62 +106,26 @@ source_group("Machine Independent\\Generated Source" FILES ${MACHINE_INDEPENDENT
 # Add system specific settings
 if (WIN32)
     set(OSDEPENDENT_FILES
-      hlslang/OSDependent/Windows/main.cpp
       hlslang/OSDependent/Windows/osinclude.h
       hlslang/OSDependent/Windows/ossource.cpp
     )
     source_group("OSDependent\\Windows" FILES ${OSDEPENDENT_FILES})
-    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hlslang/OSDependent/Windows)
+    include_directories(hlslang/OSDependent/Windows)
     
-    add_custom_command(OUTPUT hlslang/MachineIndependent/Gen_hlslang_tab.cpp hlslang/MachineIndependent/hlslang_tab.h 
-                         COMMAND set ARGS "BISON_SIMPLE=../../tools/bison.simple"
-                         COMMAND set ARGS "BISON_HAIRY=../../tools/bison.simple"
-                         COMMAND ../../tools/bison.exe ARGS  -d -t -v hlslang.y
-                         COMMAND copy ARGS /y hlslang_tab.c Gen_hlslang_tab.cpp
-                         COMMAND del ARGS hlslang_tab.c
-                         COMMAND del ARGS hlslang.output
-                         MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.y
-                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
-                         COMMENT "Executing Bison on hlslang.y"
-                      )
-                   
-    add_custom_command(OUTPUT hlslang/MachineIndependent/Gen_hlslang.cpp
-                         COMMAND ../../tools/flex.exe ARGS hlslang.l
-                         MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.l
-                         DEPENDS hlslang/MachineIndependent/hlslang_tab.h
-                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
-                         COMMENT "Executing flex on hlslang.l"
-                      )
-    
-    SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D\"_HAS_ITERATOR_DEBUGGING=0\" /D\"_SECURE_SCL=0\" /D\"_CRT_SECURE_NO_WARNINGS\"")
-    SET(TEST_LIBS opengl32.lib)
+    IF (MSVC)
+        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D\"_HAS_ITERATOR_DEBUGGING=0\" /D\"_SECURE_SCL=0\" /D\"_CRT_SECURE_NO_WARNINGS\"")
+    ENDIF (MSVC)
+    SET(TEST_LIBS opengl32 gdi32 user32)
 elseif (APPLE)
     set(OSDEPENDENT_FILES
       hlslang/OSDependent/Mac/osinclude.h
       hlslang/OSDependent/Mac/ossource.cpp
     )
     source_group("OSDependent\\Mac" FILES ${OSDEPENDENT_FILES})
-    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hlslang/OSDependent/Mac)    
-
-    add_custom_command(OUTPUT hlslang/MachineIndependent/Gen_hlslang_tab.cpp hlslang/MachineIndependent/hlslang_tab.h 
-                         COMMAND set ARGS "BISON_SIMPLE=../../tools/bison.simple"
-                         COMMAND set ARGS "BISON_HAIRY=../../tools/bison.simple"
-                         COMMAND bison ARGS -o hlslang_tab.cpp -d -t -v hlslang.y
-                         COMMAND mv hlslang_tab.hpp hlslang_tab.h
-                         MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.y
-                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
-                         COMMENT "Executing Bison on hlslang.y"
-                      )
-                   
-    add_custom_command(OUTPUT hlslang/MachineIndependent/Gen_hlslang.cpp
-                         COMMAND flex ARGS hlslang.l
-                         MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.l
-                         DEPENDS hlslang/MachineIndependent/hlslang_tab.h
-                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
-                         COMMENT "Executing flex on hlslang.l"
-                      )
+    include_directories(hlslang/OSDependent/Mac)
+
     FIND_LIBRARY(OPENGL_LIBRARY OpenGL)
-	FIND_LIBRARY(COCOA_LIBRARY Cocoa)
+    FIND_LIBRARY(COCOA_LIBRARY Cocoa)
     SET(TEST_LIBS ${OPENGL_LIBRARY} ${COCOA_LIBRARY})
 elseif (UNIX)
     set(OSDEPENDENT_FILES
@@ -170,33 +133,69 @@ elseif (UNIX)
       hlslang/OSDependent/Linux/ossource.cpp
     )
     source_group("OSDependent\\Linux" FILES ${OSDEPENDENT_FILES})
-    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/hlslang/OSDependent/Linux)
-
-    add_custom_command(OUTPUT hlslang/MachineIndependent/hlslang_tab.cpp hlslang/MachineIndependent/hlslang_tab.h 
-                         COMMAND set ARGS "BISON_SIMPLE=../../tools/bison.simple"
-                         COMMAND set ARGS "BISON_HAIRY=../../tools/bison.simple"
-                         COMMAND bison ARGS -o hlslang_tab.cpp -d -t -v hlslang.y
-                         COMMAND mv hlslang_tab.hpp hlslang_tab.h
-                         MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.y
-                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
-                         COMMENT "Executing Bison on hlslang.y"
-                      )
-                   
-    add_custom_command(OUTPUT hlslang/MachineIndependent/Gen_hlslang.cpp
-                         COMMAND flex ARGS hlslang.l
-                         MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.l
-                         DEPENDS hlslang/MachineIndependent/hlslang_tab.h
-                         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
-                         COMMENT "Executing flex on hlslang.l"
-                      )
+    include_directories(hlslang/OSDependent/Linux)
+
     SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ggdb")
     SET(TEST_LIBS GL glut GLEW pthread)
 endif ()
 
+# Bison reads two environment variables at build-time (BISON_SIMPLE and BISON_HAIRY):
+#
+# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_get_or_set_environment_variables.3F
+# "Also, environment variables SET in the CMakeLists.txt only take effect for cmake
+#  itself (configure-time), so you cannot use this method to set an environment variable
+#  that a custom command might need (build-time). Barring environment variable support
+#  by various CMake commands (e.g. add_custom_command(), currently not supported yet),
+#  an acceptable workaround may be to invoke shell scripts instead which wrap the
+#  commands to be executed."
+#
+# So making that work for both batch files and shell:
+#
+# To be precise, here we want to check for batch file
+# execution rather than that we're using the MSVC IDE
+# or are generating "MinGW Makefiles".
+#
+if ((MSVC_IDE) OR (${CMAKE_GENERATOR} STREQUAL "MinGW Makefiles"))
+    SET(SETENV "set")
+    SET(CONTINUATION &)
+else ()
+    SET(SETENV)
+    SET(CONTINUATION)
+endif ()
+
+set(ENV{BISON_SIMPLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/bison.simple)
+set(ENV{BISON_HAIRY} ${CMAKE_CURRENT_SOURCE_DIR}/tools/bison.simple)
+
+if (WIN32)
+    set(BISON_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/tools/bin/bison.exe)
+    set(FLEX_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/tools/flex.exe)
+else ()
+    find_package(BISON REQUIRED)
+    find_package(FLEX REQUIRED)
+endif ()
+
+add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent/Gen_hlslang_tab.cpp ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent/hlslang_tab.h
+                   COMMAND ${SETENV} BISON_SIMPLE=${CMAKE_CURRENT_SOURCE_DIR}/tools/bison.simple ${CONTINUATION}
+                           ${SETENV} BISON_HAIRY=${CMAKE_CURRENT_SOURCE_DIR}/tools/bison.simple ${CONTINUATION}
+                           ${BISON_EXECUTABLE} ARGS -o hlslang_tab.c -d -t -v hlslang.y
+                   COMMAND ${CMAKE_COMMAND} -E copy_if_different hlslang_tab.c Gen_hlslang_tab.cpp
+                   COMMAND ${CMAKE_COMMAND} -E remove -f hlslang_tab.c hlslang.output
+                   MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.y
+                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
+                   COMMENT "Executing Bison on hlslang.y"
+                  )
+
+add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent/Gen_hlslang.cpp
+                   COMMAND ${FLEX_EXECUTABLE} ARGS hlslang.l
+                   MAIN_DEPENDENCY hlslang/MachineIndependent/hlslang.l
+                   DEPENDS hlslang/MachineIndependent/hlslang_tab.h
+                   WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
+                   COMMENT "Executing flex on hlslang.l"
+                  )
 
 include_directories(
-  ${CMAKE_CURRENT_SOURCE_DIR}/hlslang
-  ${CMAKE_CURRENT_SOURCE_DIR}/hlslang/MachineIndependent
+  hlslang
+  hlslang/MachineIndependent
 )
 
 add_library(hlsl2glsl 
-- 
2.5.0

