Allow building of static executables with cmake.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b63d14b..974a34b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@
     cmake_policy(SET CMP0003 NEW)
 endif (COMMAND cmake_policy)
 
-SET(BUILD_SHARED_LIBS ON)
+OPTION(BUILD_SHARED_LIBS "Build shared libraries" ON)
 
 # Where to look first for cmake modules
 set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
@@ -31,7 +31,7 @@
 
 set (PACKAGE_STRING "yasm ${PACKAGE_VERSION}")
 
-INCLUDE_DIRECTORIES(BEFORE ${CMAKE_BINARY_DIR} ${yasm_SOURCE_DIR})
+INCLUDE_DIRECTORIES(AFTER ${CMAKE_BINARY_DIR} ${yasm_SOURCE_DIR})
 
 INCLUDE(ConfigureChecks.cmake)
 
diff --git a/config.h.cmake b/config.h.cmake
index 50ed93a..e002677 100644
--- a/config.h.cmake
+++ b/config.h.cmake
@@ -2,6 +2,9 @@
 
 #define CMAKE_BUILD 1
 
+/* Define if shared libs are being built */
+#cmakedefine BUILD_SHARED_LIBS 1
+
 /* Define if messsage translations are enabled */
 #cmakedefine ENABLE_NLS 1
 
diff --git a/frontends/tasm/CMakeLists.txt b/frontends/tasm/CMakeLists.txt
index 982b95d..e275ab8 100644
--- a/frontends/tasm/CMakeLists.txt
+++ b/frontends/tasm/CMakeLists.txt
@@ -15,12 +15,20 @@
 
 SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
 
-ADD_EXECUTABLE(ytasm
-    tasm.c
-    tasm-options.c
-    ${yasm_SOURCE_DIR}/frontends/yasm/yasm-plugin.c
-    )
-TARGET_LINK_LIBRARIES(ytasm libyasm ${LIBDL})
+IF(BUILD_SHARED_LIBS)
+    ADD_EXECUTABLE(ytasm
+        tasm.c
+        tasm-options.c
+        ${yasm_SOURCE_DIR}/frontends/yasm/yasm-plugin.c
+        )
+    TARGET_LINK_LIBRARIES(ytasm libyasm ${LIBDL})
+ELSE(BUILD_SHARED_LIBS)
+    ADD_EXECUTABLE(ytasm
+        tasm.c
+        tasm-options.c
+        )
+    TARGET_LINK_LIBRARIES(ytasm yasmstd libyasm)
+ENDIF(BUILD_SHARED_LIBS)
 
 SET_SOURCE_FILES_PROPERTIES(tasm.c PROPERTIES
     OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/license.c
diff --git a/frontends/tasm/tasm.c b/frontends/tasm/tasm.c
index b0b89de..58954b6 100644
--- a/frontends/tasm/tasm.c
+++ b/frontends/tasm/tasm.c
@@ -38,7 +38,7 @@
 
 #include "tasm-options.h"
 
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 #include "yasm-plugin.h"
 #endif
 
@@ -46,6 +46,10 @@
 
 #define DEFAULT_OBJFMT_MODULE   "bin"
 
+#if defined(CMAKE_BUILD) && !defined(BUILD_SHARED_LIBS)
+void yasm_init_plugin(void);
+#endif
+
 /*@null@*/ /*@only@*/ static char *obj_filename = NULL, *in_filename = NULL;
 /*@null@*/ /*@only@*/ static char *list_filename = NULL, *xref_filename = NULL;
 /*@null@*/ /*@only@*/ static char *machine_name = NULL;
@@ -457,10 +461,14 @@
 
 #ifdef CMAKE_BUILD
     /* Load standard modules */
+#ifdef BUILD_SHARED_LIBS
     if (!load_plugin("yasmstd")) {
         print_error(_("%s: could not load standard modules"), _("FATAL"));
         return EXIT_FAILURE;
     }
+#else
+    yasm_init_plugin();
+#endif
 #endif
 
     /* Initialize parameter storage */
@@ -640,7 +648,7 @@
 
     if (errfile != stderr && errfile != stdout)
         fclose(errfile);
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
     unload_plugins();
 #endif
 }
diff --git a/frontends/vsyasm/CMakeLists.txt b/frontends/vsyasm/CMakeLists.txt
index 62c7405..6815b18 100644
--- a/frontends/vsyasm/CMakeLists.txt
+++ b/frontends/vsyasm/CMakeLists.txt
@@ -15,12 +15,20 @@
 
 SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
 
-ADD_EXECUTABLE(vsyasm
-    vsyasm.c
-    ${yasm_SOURCE_DIR}/frontends/yasm/yasm-options.c
-    ${yasm_SOURCE_DIR}/frontends/yasm/yasm-plugin.c
-    )
-TARGET_LINK_LIBRARIES(vsyasm libyasm ${LIBDL})
+IF(BUILD_SHARED_LIBS)
+    ADD_EXECUTABLE(vsyasm
+        vsyasm.c
+        ${yasm_SOURCE_DIR}/frontends/yasm/yasm-options.c
+        ${yasm_SOURCE_DIR}/frontends/yasm/yasm-plugin.c
+        )
+    TARGET_LINK_LIBRARIES(vsyasm libyasm ${LIBDL})
+ELSE(BUILD_SHARED_LIBS)
+    ADD_EXECUTABLE(vsyasm
+        vsyasm.c
+        ${yasm_SOURCE_DIR}/frontends/yasm/yasm-options.c
+        )
+    TARGET_LINK_LIBRARIES(vsyasm yasmstd libyasm)
+ENDIF(BUILD_SHARED_LIBS)
 
 SET_SOURCE_FILES_PROPERTIES(vsyasm.c PROPERTIES
     OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/license.c
diff --git a/frontends/vsyasm/vsyasm.c b/frontends/vsyasm/vsyasm.c
index 4ab4636..905145f 100644
--- a/frontends/vsyasm/vsyasm.c
+++ b/frontends/vsyasm/vsyasm.c
@@ -37,12 +37,16 @@
 
 #include "frontends/yasm/yasm-options.h"
 
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 #include "frontends/yasm/yasm-plugin.h"
 #endif
 
 #include "license.c"
 
+#if defined(CMAKE_BUILD) && !defined(BUILD_SHARED_LIBS)
+void yasm_init_plugin(void);
+#endif
+
 /*@null@*/ /*@only@*/ static char *objdir_pathname = NULL;
 /*@null@*/ /*@only@*/ static char *global_prefix = NULL, *global_suffix = NULL;
 /*@null@*/ /*@only@*/ static char *listdir_pathname = NULL;
@@ -107,7 +111,7 @@
 static int opt_ewmsg_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_prefix_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_suffix_handler(char *cmd, /*@null@*/ char *param, int extra);
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 static int opt_plugin_handler(char *cmd, /*@null@*/ char *param, int extra);
 #endif
 
@@ -207,7 +211,7 @@
       N_("append argument to name of all external symbols"), N_("suffix") },
     { 0, "postfix", 1, opt_suffix_handler, 0,
       N_("append argument to name of all external symbols"), N_("suffix") },
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
     { 'N', "plugin", 1, opt_plugin_handler, 0,
       N_("load plugin module"), N_("plugin") },
 #endif
@@ -542,10 +546,14 @@
 
 #ifdef CMAKE_BUILD
     /* Load standard modules */
+#ifdef BUILD_SHARED_LIBS
     if (!load_plugin("yasmstd")) {
         print_error(_("%s: could not load standard modules"), _("FATAL"));
         return EXIT_FAILURE;
     }
+#else
+    yasm_init_plugin();
+#endif
 #endif
 
     /* Initialize parameter storage */
@@ -783,7 +791,7 @@
 
     if (errfile != stderr && errfile != stdout)
         fclose(errfile);
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
     unload_plugins();
 #endif
 }
@@ -1201,7 +1209,7 @@
     return 0;
 }
 
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 static int
 opt_plugin_handler(/*@unused@*/ char *cmd, char *param,
                    /*@unused@*/ int extra)
diff --git a/frontends/yasm/CMakeLists.txt b/frontends/yasm/CMakeLists.txt
index 6d963eb..b11d7f8 100644
--- a/frontends/yasm/CMakeLists.txt
+++ b/frontends/yasm/CMakeLists.txt
@@ -12,12 +12,20 @@
 
 SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
 
-ADD_EXECUTABLE(yasm
-    yasm.c
-    yasm-options.c
-    yasm-plugin.c
-    )
-TARGET_LINK_LIBRARIES(yasm libyasm ${LIBDL})
+IF(BUILD_SHARED_LIBS)
+    ADD_EXECUTABLE(yasm
+        yasm.c
+        yasm-options.c
+        yasm-plugin.c
+        )
+    TARGET_LINK_LIBRARIES(yasm libyasm ${LIBDL})
+ELSE(BUILD_SHARED_LIBS)
+    ADD_EXECUTABLE(yasm
+        yasm.c
+        yasm-options.c
+        )
+    TARGET_LINK_LIBRARIES(yasm yasmstd libyasm)
+ENDIF(BUILD_SHARED_LIBS)
 
 SET_SOURCE_FILES_PROPERTIES(yasm.c PROPERTIES
     OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/license.c
diff --git a/frontends/yasm/yasm.c b/frontends/yasm/yasm.c
index 4435854..ff4c59e 100644
--- a/frontends/yasm/yasm.c
+++ b/frontends/yasm/yasm.c
@@ -37,7 +37,7 @@
 
 #include "yasm-options.h"
 
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 #include "yasm-plugin.h"
 #endif
 
@@ -108,10 +108,14 @@
 static int opt_makedep_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_prefix_handler(char *cmd, /*@null@*/ char *param, int extra);
 static int opt_suffix_handler(char *cmd, /*@null@*/ char *param, int extra);
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 static int opt_plugin_handler(char *cmd, /*@null@*/ char *param, int extra);
 #endif
 
+#if defined(CMAKE_BUILD) && !defined(BUILD_SHARED_LIBS)
+void yasm_init_plugin(void);
+#endif
+
 static /*@only@*/ char *replace_extension(const char *orig, /*@null@*/
                                           const char *ext, const char *def);
 static void print_error(const char *fmt, ...);
@@ -204,7 +208,7 @@
       N_("append argument to name of all external symbols"), N_("suffix") },
     { 0, "postfix", 1, opt_suffix_handler, 0,
       N_("append argument to name of all external symbols"), N_("suffix") },
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
     { 'N', "plugin", 1, opt_plugin_handler, 0,
       N_("load plugin module"), N_("plugin") },
 #endif
@@ -611,10 +615,14 @@
 
 #ifdef CMAKE_BUILD
     /* Load standard modules */
+#ifdef BUILD_SHARED_LIBS
     if (!load_plugin("yasmstd")) {
         print_error(_("%s: could not load standard modules"), _("FATAL"));
         return EXIT_FAILURE;
     }
+#else
+    yasm_init_plugin();
+#endif
 #endif
 
     /* Initialize parameter storage */
@@ -811,7 +819,7 @@
 
     if (errfile != stderr && errfile != stdout)
         fclose(errfile);
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
     unload_plugins();
 #endif
 }
@@ -1192,7 +1200,7 @@
     return 0;
 }
 
-#ifdef CMAKE_BUILD
+#if defined(CMAKE_BUILD) && defined(BUILD_SHARED_LIBS)
 static int
 opt_plugin_handler(/*@unused@*/ char *cmd, char *param,
                    /*@unused@*/ int extra)
diff --git a/libyasm-stdint.h.cmake b/libyasm-stdint.h.cmake
index f2361d4..3759a03 100644
--- a/libyasm-stdint.h.cmake
+++ b/libyasm-stdint.h.cmake
@@ -20,8 +20,13 @@
 typedef unsigned long uintptr_t;
 #endif
 
+#ifndef BUILD_SHARED_LIBS
+#cmakedefine BUILD_SHARED_LIBS
+#define BUILD_SHARED_LIBS_UNDEF
+#endif
+
 #ifndef YASM_LIB_DECL
-# ifdef _MSC_VER
+# if defined(BUILD_SHARED_LIBS) && defined(_MSC_VER)
 #  ifdef YASM_LIB_SOURCE
 #   define YASM_LIB_DECL __declspec(dllexport)
 #  else
@@ -33,5 +38,9 @@
 #endif
 
 #undef HAVE_STDINT_H
+#ifdef BUILD_SHARED_LIBS_UNDEF
+#undef BUILD_SHARED_LIBS
+#undef BUILD_SHARED_LIBS_UNDEF
+#endif
 
 #endif
diff --git a/libyasm/CMakeLists.txt b/libyasm/CMakeLists.txt
index f886f73..bd9b7b2 100644
--- a/libyasm/CMakeLists.txt
+++ b/libyasm/CMakeLists.txt
@@ -1,6 +1,6 @@
 SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
 
-ADD_LIBRARY(libyasm SHARED
+ADD_LIBRARY(libyasm
     assocdat.c
     bitvect.c
     bc-align.c
@@ -31,10 +31,16 @@
     xmalloc.c
     xstrdup.c
     )
-SET_TARGET_PROPERTIES(libyasm PROPERTIES
-    OUTPUT_NAME "yasm"
-    COMPILE_FLAGS -DYASM_LIB_SOURCE
-    )
+IF(BUILD_SHARED_LIBS)
+    SET_TARGET_PROPERTIES(libyasm PROPERTIES
+        OUTPUT_NAME "yasm"
+        COMPILE_FLAGS -DYASM_LIB_SOURCE
+        )
+ELSE(BUILD_SHARED_LIBS)
+    SET_TARGET_PROPERTIES(libyasm PROPERTIES
+        COMPILE_FLAGS -DYASM_LIB_SOURCE
+        )
+ENDIF(BUILD_SHARED_LIBS)
 
 INSTALL(TARGETS libyasm
     RUNTIME DESTINATION bin
diff --git a/modules/CMakeLists.txt b/modules/CMakeLists.txt
index 48a3b5c..e3f8eb1 100644
--- a/modules/CMakeLists.txt
+++ b/modules/CMakeLists.txt
@@ -56,9 +56,11 @@
         FILE(APPEND ${INIT_PLUGIN_C}
              "extern yasm_${_type}_module yasm_${_keyword}_LTX_${_type};\n")
     ENDFOREACH(module)
-    FILE(APPEND ${INIT_PLUGIN_C} "\n#ifdef _MSC_VER\n")
-    FILE(APPEND ${INIT_PLUGIN_C} "__declspec(dllexport)\n")
-    FILE(APPEND ${INIT_PLUGIN_C} "#endif\n")
+    IF(BUILD_SHARED_LIBS)
+        FILE(APPEND ${INIT_PLUGIN_C} "\n#ifdef _MSC_VER\n")
+        FILE(APPEND ${INIT_PLUGIN_C} "__declspec(dllexport)\n")
+        FILE(APPEND ${INIT_PLUGIN_C} "#endif\n")
+    ENDIF(BUILD_SHARED_LIBS)
     FILE(APPEND ${INIT_PLUGIN_C} "void\n")
     FILE(APPEND ${INIT_PLUGIN_C} "yasm_init_plugin(void)\n")
     FILE(APPEND ${INIT_PLUGIN_C} "{\n")
@@ -80,14 +82,22 @@
 
 SET(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
 
-ADD_LIBRARY(yasmstd MODULE
-    init_plugin.c
-    ${YASM_MODULES_SRC}
-    )
-TARGET_LINK_LIBRARIES(yasmstd libyasm)
+IF(BUILD_SHARED_LIBS)
+    ADD_LIBRARY(yasmstd MODULE
+        init_plugin.c
+        ${YASM_MODULES_SRC}
+        )
+    TARGET_LINK_LIBRARIES(yasmstd libyasm)
 
-IF(WIN32)
-    INSTALL(TARGETS yasmstd LIBRARY DESTINATION bin)
-ELSE(WIN32)
-    INSTALL(TARGETS yasmstd LIBRARY DESTINATION lib)
-ENDIF(WIN32)
+    IF(WIN32)
+        INSTALL(TARGETS yasmstd LIBRARY DESTINATION bin)
+    ELSE(WIN32)
+        INSTALL(TARGETS yasmstd LIBRARY DESTINATION lib)
+    ENDIF(WIN32)
+ELSE(BUILD_SHARED_LIBS)
+    ADD_LIBRARY(yasmstd
+        init_plugin.c
+        ${YASM_MODULES_SRC}
+        )
+    TARGET_LINK_LIBRARIES(yasmstd libyasm)
+ENDIF(BUILD_SHARED_LIBS)