Bug 1077148 part 1 - Define mozcrt in moz.build. r=gps
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 30 Oct 2014 13:05:50 +0900
changeset 237374 7c2f1416473fc4c03f96299a68ba869de9467b48
parent 237373 ac0d7bf37abced5397e34e1f1a5ccc3413c0a115
child 237375 a7b5bb9e867a583d01513acb8ca4431cc9acb3d1
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps
bugs1077148
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1077148 part 1 - Define mozcrt in moz.build. r=gps
config/external/nss/Makefile.in
config/external/nss/moz.build
configure.in
js/src/configure.in
mozglue/build/Makefile.in
mozglue/build/fixcrt.py
mozglue/crt/Makefile.in
mozglue/crt/fixcrt.py
mozglue/crt/moz.build
mozglue/moz.build
python/mozbuild/mozbuild/backend/recursivemake.py
--- a/config/external/nss/Makefile.in
+++ b/config/external/nss/Makefile.in
@@ -5,16 +5,22 @@
 
 ifndef MOZ_NATIVE_NSS
 
 CC_WRAPPER =
 CXX_WRAPPER =
 
 default::
 
+# When MOZ_CRT is set, we get mozcrt from moz.build instead of
+# MOZ_GLUE_LDFLAGS
+ifdef MOZ_CRT
+MOZ_GLUE_LDFLAGS =
+endif
+
 include $(topsrcdir)/config/makefiles/functions.mk
 
 NSS_LIBS = \
   nss3 \
   nssutil3 \
   smime3 \
   ssl3 \
   $(NULL)
@@ -103,21 +109,16 @@ else
 NSPR_INCLUDE_DIR = $(ABS_DIST)/include/nspr
 endif
 NSPR_LIB_DIR = $(firstword $(filter -L%,$(NSPR_LIBS)))
 ifneq (,$(strip $(NSPR_LIB_DIR)))
 NSPR_LIB_DIR := $(subst -L,,$(subst -L$(DIST),-L$(ABS_DIST),$(NSPR_LIB_DIR)))
 else
 NSPR_LIB_DIR = $(ABS_DIST)/lib
 endif
-# Can't pass this in DEFAULT_GMAKE_FLAGS because that overrides
-# definitions in NSS, so just export it into the sub-make's environment.
-ifeq (WINNT_1,$(OS_TARGET)_$(MOZ_MEMORY))
-export DLLFLAGS
-endif
 
 # To get debug symbols from NSS
 export MOZ_DEBUG_SYMBOLS
 
 DEFAULT_GMAKE_FLAGS =
 DEFAULT_GMAKE_FLAGS += CC='$(CC)'
 DEFAULT_GMAKE_FLAGS += SOURCE_MD_DIR=$(ABS_DIST)
 DEFAULT_GMAKE_FLAGS += SOURCE_MDHEADERS_DIR=$(NSPR_INCLUDE_DIR)
@@ -352,16 +353,28 @@ GARBAGE += \
   $(NULL)
 endif # GCC_USE_GNU_LD
 endif # WINNT
 
 endif # MOZ_FOLD_LIBS
 
 include $(topsrcdir)/config/rules.mk
 
+# Can't pass this in DEFAULT_GMAKE_FLAGS because that overrides
+# definitions in NSS, so just export it into the sub-make's environment.
+ifeq (WINNT_1,$(OS_TARGET)_$(MOZ_MEMORY))
+ifdef MOZ_CRT
+# OS_LIBS comes from having mozcrt as a dependency in moz.build.
+DLLFLAGS := $(OS_LIBS)
+else
+DLLFLAGS := -LIBPATH:$(ABS_DIST)/lib -DEFAULTLIB:mozglue
+endif
+export DLLFLAGS
+endif
+
 ifdef MOZ_FOLD_LIBS
 # Force the linker to include everything from the static libraries.
 EXPAND_LIBS_EXEC += --extract
 
 $(SHARED_LIBRARY): $(addprefix $(DEPTH)/security/,$(NSS_STATIC_LIBS))
 
 ifdef IMPORT_LIB_SUFFIX
 IMPORT_PREFIX = $(LIB_PREFIX)
--- a/config/external/nss/moz.build
+++ b/config/external/nss/moz.build
@@ -31,16 +31,22 @@ elif CONFIG['MOZ_FOLD_LIBS']:
 
     OS_LIBS += CONFIG['REALTIME_LIBS']
 
     if CONFIG['OS_TARGET'] == 'WINNT':
         DEFFILE = 'nss3.def'
 
     if CONFIG['OS_ARCH'] == 'Linux' and CONFIG['GCC_USE_GNU_LD']:
         LD_VERSION_SCRIPT = 'nss3.def'
+
+    # mozcrt needs at least one reference in a moz.build for the frontend
+    # not to raise an error. Here, it allows to avoid setting DLLFLAGS in
+    # configure.in.
+    if CONFIG['MOZ_CRT']:
+        USE_LIBS += ['mozcrt']
 else:
     Library('nss')
     USE_LIBS += [
         '/security/nss/lib/nss/nss3',
         '/security/nss/lib/smime/smime3',
         '/security/nss/lib/ssl/ssl3',
         '/security/nss/lib/util/nssutil3',
         'sqlite',
--- a/configure.in
+++ b/configure.in
@@ -7235,37 +7235,34 @@ else
   *-mingw*)
     AC_DEFINE(MOZ_MEMORY_WINDOWS)
     export MOZ_NO_DEBUG_RTL=1
     WIN32_CRT_LIBS="msvcrt.lib msvcprt.lib"
     dnl Look for a broken crtdll.obj
     WIN32_CRTDLL_FULLPATH=`lib -nologo -list $WIN32_CRT_LIBS | grep crtdll\\.obj`
     lib -NOLOGO -OUT:crtdll.obj $WIN32_CRT_LIBS -EXTRACT:$WIN32_CRTDLL_FULLPATH
     if grep -q '__imp__\{0,1\}free' crtdll.obj; then
+      MOZ_CRT=1
       MOZ_GLUE_LDFLAGS='-LIBPATH:$(DIST)/lib -NODEFAULTLIB:msvcrt -NODEFAULTLIB:msvcprt -DEFAULTLIB:mozcrt'
-      dnl Also pass this to NSPR/NSS
-      DLLFLAGS="$DLLFLAGS $MOZ_GLUE_LDFLAGS"
-    else
-      DLLFLAGS="$DLLFLAGS -LIBPATH:\$(DIST)/lib -DEFAULTLIB:mozglue"
     fi
     rm crtdll.obj
     ;;
   *)
     AC_MSG_ERROR([--enable-jemalloc not supported on ${target}])
     ;;
   esac
 fi # MOZ_MEMORY
 AC_SUBST(MOZ_MEMORY)
 AC_SUBST(MOZ_JEMALLOC3)
 AC_SUBST(MOZ_NATIVE_JEMALLOC)
+AC_SUBST(MOZ_CRT)
+export MOZ_CRT
 AC_SUBST(MOZ_GLUE_LDFLAGS)
 AC_SUBST(MOZ_GLUE_PROGRAM_LDFLAGS)
 AC_SUBST_LIST(WIN32_CRT_LIBS)
-dnl Need to set this for make because NSS doesn't have configure
-AC_SUBST(DLLFLAGS)
 
 dnl We need to wrap dlopen and related functions on Android because we use
 dnl our own linker.
 if test "$OS_TARGET" = Android; then
     WRAP_LDFLAGS="${WRAP_LDFLAGS} -L$_objdir/dist/lib -lmozglue"
     WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=PR_GetEnv,--wrap=PR_SetEnv"
     if test "$MOZ_WIDGET_TOOLKIT" = gonk -a -n "$MOZ_NUWA_PROCESS"; then
         WRAP_LDFLAGS="${WRAP_LDFLAGS} -Wl,--wrap=pthread_create,--wrap=epoll_wait,--wrap=poll,--wrap=pthread_cond_timedwait,--wrap=__pthread_cond_timedwait,--wrap=pthread_cond_wait,--wrap=epoll_create,--wrap=epoll_ctl,--wrap=close,--wrap=pthread_key_create,--wrap=pthread_key_delete,--wrap=socketpair,--wrap=pthread_self,--wrap=pthread_mutex_lock,--wrap=pthread_join,--wrap=pipe,--wrap=pipe2,--wrap=tgkill"
--- a/js/src/configure.in
+++ b/js/src/configure.in
@@ -3116,16 +3116,17 @@ if test "$MOZ_MEMORY"; then
     # the interesting bits will get passed down in MOZ_GLUE_LDFLAGS
     ;;
   *)
     AC_MSG_ERROR([--enable-jemalloc not supported on ${target}])
     ;;
   esac
 fi
 AC_SUBST(MOZ_MEMORY)
+AC_SUBST(MOZ_CRT)
 AC_SUBST(MOZ_GLUE_LDFLAGS)
 AC_SUBST(MOZ_GLUE_PROGRAM_LDFLAGS)
 
 dnl ========================================================
 dnl = Use malloc wrapper lib
 dnl ========================================================
 MOZ_ARG_ENABLE_BOOL(wrap-malloc,
 [  --enable-wrap-malloc    Wrap malloc calls (gnu linker only)],
--- a/mozglue/build/Makefile.in
+++ b/mozglue/build/Makefile.in
@@ -11,17 +11,17 @@ include $(topsrcdir)/config/config.mk
 MOZ_GLUE_LDFLAGS = # Don't link against ourselves
 
 ifeq (WINNT,$(OS_TARGET))
 mozglue.def: mozglue.def.in $(GLOBAL_DEPS)
 	$(call py_action,preprocessor,$(if $(MOZ_REPLACE_MALLOC),-DMOZ_REPLACE_MALLOC) $(ACDEFINES) $< -o $@)
 
 GARBAGE += mozglue.def
 
-ifneq (,$(filter -DEFAULTLIB:mozcrt,$(MOZ_GLUE_LDFLAGS)))
+ifdef MOZ_CRT
 # Don't install the import library if we use mozcrt
 NO_INSTALL_IMPORT_LIBRARY = 1
 endif
 
 endif
 
 include $(topsrcdir)/mozglue/build/replace_malloc.mk
 
@@ -30,91 +30,8 @@ ifeq (arm, $(TARGET_CPU))
 OS_LDFLAGS += -Wl,-version-script,$(srcdir)/arm-eabi-filter
 endif
 
 endif
 
 ifeq (Android, $(OS_TARGET))
 WRAP_LDFLAGS := $(filter -Wl%,$(WRAP_LDFLAGS))
 endif
-
-include $(topsrcdir)/config/rules.mk
-
-ifdef MOZ_MEMORY
-ifeq (WINNT,$(OS_TARGET))
-# Roll our own custom logic here for the import library
-
-###############################################################################
-#
-# Linking Mozilla itself to jemalloc is not particularly difficult.  To do this
-# we avoid linking directly to the Microsoft-provided CRT import libraries.
-# Instead, we link to our own import library which we generate here.  To
-# replace the CRT's malloc/free/other memory management symbols we export
-# our own versions out of jemalloc.dll.  We then take the import library that
-# the compiler generates for jemalloc.dll and combine it with the MS CRT import
-# libraries.  We put our library on the command line first, and the CRT symbols
-# are discarded in favor of our versions!
-#
-# Unfortunately that was too easy.  The CRT import library is not a standard
-# import library that contains a list of symbols and whatnot.  It also includes
-# object files that are linked into generated programs.  One of these,
-# crtdll.obj is (as one might expect) linked into all DLLs that link against
-# the CRT.  This file does things like run static C++ constructors when the
-# DLL is attached, call DllMain, etc.
-#
-# In the CRT source all malloc/free calls are made to malloc_crt and free_crt.
-# In debug builds these are both defined to malloc_dbg and free_dbg.  In opt
-# builds malloc_crt is an actual function, implemented and exposed from the
-# CRT.  free_crt is, however, defined to be just plain old free.  This works
-# fine inside the CRT where malloc_crt and free operate on the same heap.
-# Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's
-# heap.  This causes much pain at shutdown :-(
-#
-# The obvious solution here is to override malloc_crt too.  Unfortunately,
-# that doesn't work because the CRT expects to be able to call msize on this
-# piece of memory deep inside the CRT, which will fail because it'll call the
-# CRT's msize on a pointer in jemalloc's heap.
-#
-# Our solution to this is quite devious.  We take apart the CRT's import lib
-# and remove the problematic object file.  We then poke at the object file's
-# symbol table and replace '__imp__free' (which means grab free from some
-# other DLL) with '__imp__frex'.  Then we define our own dummy no-op function
-# in jemalloc.dll and export it as frex.  Then we put the CRT import lib
-# back together with the patched crtdll.obj, glue it to the end of jemalloc's
-# import library and link the rest of Mozilla to that.
-#
-# The result?  A binary that uses jemalloc, doesn't crash, and leaks a tiny
-# amount of memory (32 words per DLL in the 2010 CRT) at shutdown.
-#
-###############################################################################
-
-target:: mozcrt.lib
-	$(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib
-
-# And finally combine that with the jemalloc import library to get an import
-# library that has our malloc/free/etc and the CRT's everything else
-mozcrt.lib: $(IMPORT_LIBRARY) msvc_modified.lib
-	lib -OUT:$@ $^
-
-# Put the fixed object file back in
-msvc_modified.lib: msvc_removed.lib crtdll_fixed.obj
-	lib -OUT:$@ $^
-
-# Fix the object file
-crtdll_fixed.obj: crtdll.obj
-	$(PYTHON) $(srcdir)/fixcrt.py
-
-# Find the path of crtdll.obj
-CRTDLL_FULLPATH=$(subst \,\\,$(shell lib -list msvc_combined.lib | grep crtdll\\.obj))
-
-# Remove the broken object file, only after we have extracted it
-msvc_removed.lib: msvc_combined.lib crtdll.obj
-	lib -OUT:$@ msvc_combined.lib -REMOVE:$(CRTDLL_FULLPATH)
-
-# Extract the broken object file out of the combined library
-crtdll.obj: msvc_combined.lib
-	lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH)
-
-# Grab both CRT libraries and combine them into one library to simplify things
-msvc_combined.lib:
-	lib -OUT:$@ $(WIN32_CRT_LIBS)
-endif
-endif # MOZ_MEMORY
copy from mozglue/build/Makefile.in
copy to mozglue/crt/Makefile.in
--- a/mozglue/build/Makefile.in
+++ b/mozglue/crt/Makefile.in
@@ -1,50 +1,13 @@
 #
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
-DIST_INSTALL = 1
-
-# For FORCE_SHARED_LIB
-include $(topsrcdir)/config/config.mk
-
-MOZ_GLUE_LDFLAGS = # Don't link against ourselves
-
-ifeq (WINNT,$(OS_TARGET))
-mozglue.def: mozglue.def.in $(GLOBAL_DEPS)
-	$(call py_action,preprocessor,$(if $(MOZ_REPLACE_MALLOC),-DMOZ_REPLACE_MALLOC) $(ACDEFINES) $< -o $@)
-
-GARBAGE += mozglue.def
-
-ifneq (,$(filter -DEFAULTLIB:mozcrt,$(MOZ_GLUE_LDFLAGS)))
-# Don't install the import library if we use mozcrt
-NO_INSTALL_IMPORT_LIBRARY = 1
-endif
-
-endif
-
-include $(topsrcdir)/mozglue/build/replace_malloc.mk
-
-ifdef MOZ_LINKER
-ifeq (arm, $(TARGET_CPU))
-OS_LDFLAGS += -Wl,-version-script,$(srcdir)/arm-eabi-filter
-endif
-
-endif
-
-ifeq (Android, $(OS_TARGET))
-WRAP_LDFLAGS := $(filter -Wl%,$(WRAP_LDFLAGS))
-endif
-
-include $(topsrcdir)/config/rules.mk
-
-ifdef MOZ_MEMORY
-ifeq (WINNT,$(OS_TARGET))
 # Roll our own custom logic here for the import library
 
 ###############################################################################
 #
 # Linking Mozilla itself to jemalloc is not particularly difficult.  To do this
 # we avoid linking directly to the Microsoft-provided CRT import libraries.
 # Instead, we link to our own import library which we generate here.  To
 # replace the CRT's malloc/free/other memory management symbols we export
@@ -86,17 +49,17 @@ ifeq (WINNT,$(OS_TARGET))
 #
 ###############################################################################
 
 target:: mozcrt.lib
 	$(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib
 
 # And finally combine that with the jemalloc import library to get an import
 # library that has our malloc/free/etc and the CRT's everything else
-mozcrt.lib: $(IMPORT_LIBRARY) msvc_modified.lib
+mozcrt.lib: ../build/mozglue.lib msvc_modified.lib
 	lib -OUT:$@ $^
 
 # Put the fixed object file back in
 msvc_modified.lib: msvc_removed.lib crtdll_fixed.obj
 	lib -OUT:$@ $^
 
 # Fix the object file
 crtdll_fixed.obj: crtdll.obj
@@ -111,10 +74,8 @@ msvc_removed.lib: msvc_combined.lib crtd
 
 # Extract the broken object file out of the combined library
 crtdll.obj: msvc_combined.lib
 	lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH)
 
 # Grab both CRT libraries and combine them into one library to simplify things
 msvc_combined.lib:
 	lib -OUT:$@ $(WIN32_CRT_LIBS)
-endif
-endif # MOZ_MEMORY
rename from mozglue/build/fixcrt.py
rename to mozglue/crt/fixcrt.py
new file mode 100644
--- /dev/null
+++ b/mozglue/crt/moz.build
@@ -0,0 +1,16 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+Library('mozcrt')
+
+STATIC_LIBRARY_NAME = 'mozcrt_s'
+
+OS_LIBS += [
+    '-NODEFAULTLIB:msvcrt',
+    '-NODEFAULTLIB:msvcprt',
+    '-LIBPATH:%s/mozglue/crt' % TOPOBJDIR,
+    '-DEFAULTLIB:mozcrt',
+]
--- a/mozglue/moz.build
+++ b/mozglue/moz.build
@@ -7,9 +7,12 @@
 if CONFIG['MOZ_LINKER']:
     DIRS += ['linker']
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
     DIRS += ['android']
 
 DIRS += ['build']
 
+if CONFIG['MOZ_CRT']:
+    DIRS += ['crt']
+
 TEST_DIRS += ['tests']
--- a/python/mozbuild/mozbuild/backend/recursivemake.py
+++ b/python/mozbuild/mozbuild/backend/recursivemake.py
@@ -1153,21 +1153,25 @@ INSTALL_TARGETS += %(prefix)s
         # This will create the node even if there aren't any linked libraries.
         build_target = self._build_target_for_obj(obj)
         self._compile_graph[build_target]
 
         # Until MOZ_GLUE_LDFLAGS/MOZ_GLUE_PROGRAM_LDFLAGS are properly
         # handled in moz.build world, assume any program or shared library
         # we build depends on it.
         if obj.KIND == 'target' and not isinstance(obj, StaticLibrary) and \
-                build_target != 'mozglue/build/target' and \
+                build_target not in ('mozglue/build/target',
+                                     'mozglue/crt/target') and \
                 not obj.config.substs.get('JS_STANDALONE') and \
                 (not isinstance(obj, SharedLibrary) or
                  obj.basename != 'clang-plugin'):
-            self._compile_graph[build_target].add('mozglue/build/target')
+            if obj.config.substs.get('MOZ_CRT'):
+                self._compile_graph[build_target].add('mozglue/crt/target')
+            else:
+                self._compile_graph[build_target].add('mozglue/build/target')
             if obj.config.substs.get('MOZ_MEMORY'):
                 self._compile_graph[build_target].add('memory/build/target')
 
         for lib in obj.linked_libraries:
             if not isinstance(lib, ExternalLibrary):
                 self._compile_graph[build_target].add(
                     self._build_target_for_obj(lib))
             relpath = pretty_relpath(lib)