PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/memory/jemalloc/Makefile.in

https://github.com/Akin-Net/mozilla-central
Autoconf | 248 lines | 99 code | 41 blank | 108 comment | 2 complexity | e5b89979e72b7635f3e565adb3e98d46 MD5 | raw file
  1. #
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is mozilla.org code.
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Mozilla Foundation
  19. # Portions created by the Initial Developer are Copyright (C) 2008
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # Ted Mielczarek <ted.mielczarek@gmail.com>
  24. #
  25. # Alternatively, the contents of this file may be used under the terms of
  26. # either of the GNU General Public License Version 2 or later (the "GPL"),
  27. # or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. # in which case the provisions of the GPL or the LGPL are applicable instead
  29. # of those above. If you wish to allow use of your version of this file only
  30. # under the terms of either the GPL or the LGPL, and not to allow others to
  31. # use your version of this file under the terms of the MPL, indicate your
  32. # decision by deleting the provisions above and replace them with the notice
  33. # and other provisions required by the GPL or the LGPL. If you do not delete
  34. # the provisions above, a recipient may use your version of this file under
  35. # the terms of any one of the MPL, the GPL or the LGPL.
  36. #
  37. # ***** END LICENSE BLOCK *****
  38. DEPTH = ../..
  39. topsrcdir = @top_srcdir@
  40. srcdir = @srcdir@
  41. VPATH = @srcdir@
  42. include $(DEPTH)/config/autoconf.mk
  43. MODULE = jemalloc
  44. # jemalloc.c properly uses 'static', so don't burden it with manually exposing
  45. # symbols.
  46. VISIBILITY_FLAGS=
  47. ifeq (WINNT,$(OS_TARGET))
  48. ifdef WIN32_OLD_STYLE_JEMALLOC
  49. # Building the CRT from source
  50. CRT_OBJ_DIR = $(CURDIR)/crtsrc
  51. MOZ_CRT_DLL_NAME = mozcrt19
  52. MOZ_CRTCPP_DLL_NAME = mozcpp19
  53. MOZ_CRT_STATIC_LIBS = libcmt libcpmt
  54. MOZ_CRT_DLL = $(CRT_OBJ_DIR)/build/$(MOZ_CRT_CPU_ARCH)/$(MOZ_CRT_DLL_NAME).dll
  55. MOZ_CRT_IMPORT_LIB = $(CRT_OBJ_DIR)/build/$(MOZ_CRT_CPU_ARCH)/$(MOZ_CRT_DLL_NAME).lib
  56. MOZ_CRTCPP_DLL = $(CRT_OBJ_DIR)/build/$(MOZ_CRT_CPU_ARCH)/$(MOZ_CRTCPP_DLL_NAME).dll
  57. MOZ_CRTCPP_IMPORT_LIB = $(CRT_OBJ_DIR)/build/$(MOZ_CRT_CPU_ARCH)/$(MOZ_CRTCPP_DLL_NAME).lib
  58. # copy the CRT DLLs to dist/bin,
  59. # copy the import libs to dist/lib
  60. libs:: $(MOZ_CRT_DLL) $(MOZ_CRT_IMPORT_LIB)
  61. $(INSTALL) $(MOZ_CRT_DLL) $(MOZ_CRTCPP_DLL) $(FINAL_TARGET)
  62. $(INSTALL) $(MOZ_CRT_IMPORT_LIB) $(MOZ_CRTCPP_IMPORT_LIB) $(DIST)/lib
  63. $(MOZ_CRT_IMPORT_LIB): $(MOZ_CRT_DLL)
  64. define EXTRACT_CMD
  65. cd $(CRT_OBJ_DIR)/$(MOZ_CRT_CPU_ARCH)/$(i)_lib && lib "-extract:..\\build\\$(MOZ_CRT_CPU_ARCH)\\$(i)_obj\\unhandld.obj" eh.lib
  66. endef # don't touch the blank line. actually, don't touch anything in this file.
  67. # patch if necessary
  68. ifeq ($(CC_VERSION), 14.00.50727.762)
  69. CRTDIFF=crtvc8sp1-$(MOZ_CRT_CPU_ARCH).diff
  70. else
  71. CRTDIFF=crtvc9sp1-$(MOZ_CRT_CPU_ARCH).diff
  72. endif
  73. $(CRT_OBJ_DIR)/jemalloc.c: $(srcdir)/$(CRTDIFF)
  74. rm -rf $(CRT_OBJ_DIR)
  75. cp -R "$(WIN32_CRT_SRC_DIR)" $(CRT_OBJ_DIR)
  76. # per http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1189363&SiteID=1
  77. $(foreach i,dll mt xdll xmt,$(EXTRACT_CMD))
  78. # truly awful
  79. #XXX: get ed into mozillabuild, bug 415123
  80. $(PERL) $(srcdir)/apply-ed-patches.pl $(srcdir)/$(CRTDIFF) \
  81. $(CRT_OBJ_DIR) $(srcdir)/ed.exe
  82. $(MOZ_CRT_DLL): \
  83. $(CRT_OBJ_DIR)/jemalloc.c $(srcdir)/jemalloc.c $(srcdir)/jemalloc.h \
  84. $(srcdir)/jemalloc_types.h $(srcdir)/rb.h
  85. cp $(srcdir)/jemalloc.c $(srcdir)/jemalloc.h $(srcdir)/jemalloc_types.h $(srcdir)/rb.h \
  86. $(CRT_OBJ_DIR)
  87. # this pretty much sucks, but nmake and make don't play well together
  88. $(PYTHON) $(srcdir)/build-crt.py $(CRT_OBJ_DIR)
  89. # XXX: these don't link right for some reason; the problem is likely
  90. # that not all the standard symbols are exported; looks like MSFT
  91. # never updated the sample.def files; could probably fix if someone
  92. # were ever bored enough. :-)
  93. rm -f $(addsuffix .lib, $(addprefix $(CRT_OBJ_DIR)/build/$(MOZ_CRT_CPU_ARCH)/, $(MOZ_CRT_STATIC_LIBS)))
  94. rm -f $(addsuffix .pdb, $(addprefix $(CRT_OBJ_DIR)/build/$(MOZ_CRT_CPU_ARCH)/, $(MOZ_CRT_STATIC_LIBS)))
  95. # but still export jemalloc.h
  96. EXPORTS = jemalloc.h jemalloc_types.h
  97. else
  98. CSRCS = jemalloc.c
  99. EXPORTS = jemalloc.h jemalloc_types.h
  100. LIBRARY_NAME = jemalloc
  101. FORCE_SHARED_LIB = 1
  102. MOZ_MEMORY_LDFLAGS = # Don't link against ourselves
  103. DEFFILE = $(srcdir)/jemalloc.def
  104. LDFLAGS += -ENTRY:DllMain
  105. NO_INSTALL_IMPORT_LIBRARY = 1
  106. endif
  107. else # Not Windows
  108. MODULE_OPTIMIZE_FLAGS = -O2
  109. ifeq ($(OS_ARCH),SunOS)
  110. ifndef GNU_CC
  111. MODULE_OPTIMIZE_FLAGS = -xO5
  112. endif
  113. endif
  114. LIBRARY_NAME = jemalloc
  115. # Build jemalloc as a shared lib. This is mandatory for Darwin, since a library
  116. # init function is used on that platform.
  117. ifeq ($(OS_ARCH),Darwin)
  118. FORCE_SHARED_LIB= 1
  119. else
  120. DIST_INSTALL = 1
  121. FORCE_STATIC_LIB= 1
  122. endif
  123. EXPORTS = jemalloc.h jemalloc_types.h
  124. CSRCS = jemalloc.c
  125. #XXX: PGO on Linux causes problems here
  126. # See bug 419470
  127. NO_PROFILE_GUIDED_OPTIMIZE = 1
  128. endif
  129. ifdef WRAP_MALLOC_CFLAGS
  130. DEFINES += -DWRAP_MALLOC
  131. endif
  132. include $(topsrcdir)/config/rules.mk
  133. ifeq (Darwin,$(OS_TARGET))
  134. LDFLAGS += -init _jemalloc_darwin_init
  135. endif
  136. ifeq (WINNT,$(OS_TARGET))
  137. ifndef WIN32_OLD_STYLE_JEMALLOC
  138. # Roll our own custom logic here for the import library
  139. ###############################################################################
  140. #
  141. # Linking Mozilla itself to jemalloc is not particularly difficult. To do this
  142. # we avoid linking directly to the Microsoft-provided CRT import libraries.
  143. # Instead, we link to our own import library which we generate here. To
  144. # replace the CRT's malloc/free/other memory management symbols we export
  145. # our own versions out of jemalloc.dll. We then take the import library that
  146. # the compiler generates for jemalloc.dll and combine it with the MS CRT import
  147. # libraries. We put our library on the command line first, and the CRT symbols
  148. # are discarded in favor of our versions!
  149. #
  150. # Unfortunately that was too easy. The CRT import library is not a standard
  151. # import library that contains a list of symbols and whatnot. It also includes
  152. # object files that are linked into generated programs. One of these,
  153. # crtdll.obj is (as one might expect) linked into all DLLs that link against
  154. # the CRT. This file does things like run static C++ constructors when the
  155. # DLL is attached, call DllMain, etc.
  156. #
  157. # In the CRT source all malloc/free calls are made to malloc_crt and free_crt.
  158. # In debug builds these are both defined to malloc_dbg and free_dbg. In opt
  159. # builds malloc_crt is an actual function, implemented and exposed from the
  160. # CRT. free_crt is, however, defined to be just plain old free. This works
  161. # fine inside the CRT where malloc_crt and free operate on the same heap.
  162. # Outside the CRT malloc_crt is in the CRT's heap, but free is in jemalloc's
  163. # heap. This causes much pain at shutdown :-(
  164. #
  165. # The obvious solution here is to override malloc_crt too. Unfortunately,
  166. # that doesn't work because the CRT expects to be able to call msize on this
  167. # piece of memory deep inside the CRT, which will fail because it'll call the
  168. # CRT's msize on a pointer in jemalloc's heap.
  169. #
  170. # Our solution to this is quite devious. We take apart the CRT's import lib
  171. # and remove the problematic object file. We then poke at the object file's
  172. # symbol table and replace '__imp__free' (which means grab free from some
  173. # other DLL) with '__imp__frex'. Then we define our own dummy no-op function
  174. # in jemalloc.dll and export it as frex. Then we put the CRT import lib
  175. # back together with the patched crtdll.obj, glue it to the end of jemalloc's
  176. # import library and link the rest of Mozilla to that.
  177. #
  178. # The result? A binary that uses jemalloc, doesn't crash, and leaks a tiny
  179. # amount of memory (32 words per DLL in the 2010 CRT) at shutdown.
  180. #
  181. ###############################################################################
  182. libs:: $(DIST)/lib/mozcrt.lib
  183. $(DIST)/lib/mozcrt.lib:: mozcrt.lib
  184. $(INSTALL) $(IFLAGS2) mozcrt.lib $(DIST)/lib
  185. # And finally combine that with the jemalloc import library to get an import
  186. # library that has our malloc/free/etc and the CRT's everything else
  187. mozcrt.lib:: $(IMPORT_LIBRARY) msvc_modified.lib
  188. lib -OUT:$@ $^
  189. # Put the fixed object file back in
  190. msvc_modified.lib:: msvc_removed.lib crtdll_fixed.obj
  191. lib -OUT:$@ $^
  192. # Fix the object file
  193. crtdll_fixed.obj:: crtdll.obj
  194. $(PYTHON) $(srcdir)/fixcrt.py
  195. # Find the path of crtdll.obj
  196. CRTDLL_FULLPATH=$(subst \,\\,$(shell lib -list msvc_combined.lib | grep crtdll\\.obj))
  197. # Remove the broken object file, only after we have extracted it
  198. msvc_removed.lib:: msvc_combined.lib crtdll.obj
  199. lib -OUT:$@ msvc_combined.lib -REMOVE:$(CRTDLL_FULLPATH)
  200. # Extract the broken object file out of the combined library
  201. crtdll.obj:: msvc_combined.lib
  202. lib -OUT:$@ $^ -EXTRACT:$(CRTDLL_FULLPATH)
  203. # Grab both CRT libraries and combine them into one library to simplify things
  204. msvc_combined.lib::
  205. lib -OUT:$@ $(WIN32_CRT_LIBS)
  206. endif
  207. endif