Building Firefox on Windows with clang-cl

Over the past three weeks or so, Jeff Muizelaar and I started to investigate what it would take for us to be able to use clang-cl to build Firefox on Windows, and I’m really excited to report that as of earlier this week, all of the patches required have landed on both mozilla-central and LLVM and we can produce debug builds that run at least for basic browsing.

clang-cl is the LLVM project’s drop-in replacement for Microsoft’s Visual C++ compiler.  It uses the same base technology as clang, but it supports the Microsoft ABI to a large extent, and it also provides a compiler driver that accepts many of the same options as Visual C++.  There are of course things that are missing from clang-cl still (such as support for C++ and structured exceptions, and MSVC-style inline assembly), but fortunately clang-cl supports a fallback mode which enables us to build the tiny portions of our tree which still cannot be compiled with clang-cl itself by calling back to the Microsoft compiler.  Because both compilers generate code adhering to the same ABI, we can link all of this code and produce a working build.

To try this out yourself, you’ll need to download and build clang on Windows from the recent trunk (note that we have had to fix a bunch of issues in clang/LLVM, so you really want the tip of tree trunk!).  Once you have a working clang-cl build, you need to build a recent mozilla-central tree with a mozconfig containing the following:

export CC=clang-cl
export CXX=clang-cl
export CFLAGS="-fallback"
export CXXFLAGS="-fallback"
export MOZ_CFLAGS_NSS="-fallback"

With this, you should be able to build Firefox with clang-cl!  (Note that earlier today I found a regression in clang-cl which causes a crash when configuring ICU, but you should be able to ignore that crash and proceed.)

I think clang-cl can be really interesting for Mozilla for a number of reasons.  There is of course the promise of a decent open source compiler on Windows, which really excites me, but we’re still a bit far from being able to use this compiler for our production builds.  But in the very near future, we should be able to use clang-cl in order to produce AddressSanitizer builds on Windows.  That should be super helpful to debug memory correctness issues on Windows, where we don’t really have a decent story yet.  We should also be able to experiment with other sanitizer tools in clang on Windows.  We can even start to think about other clang-based tools on Windows (DXR comes to mind.)  Soon we will also be able to start comparing the performance of the generated code between clang-cl and Visual C++ and hopefully be able to provide good test cases for the LLVM project to improve their codegen performance to make it on par with Visual C++.  And who knows what other ideas people will come up with!

Last but not least, I’ve been impressed with the quality of clang-cl.  It was relatively easy for me and Jeff to spend some of our free time over the course of 2-3 weeks to get to working build from scratch with some fixes both on our side and on LLVM.  The LLVM engineers at Google who are working on clang-cl deserve most of the credit for that.  Specifically I’d like to thank Hans Wennborg and Reid Kleckner for their immense help in fixing the LLVM issues we encountered.

If you’d like to follow along, please watch the tracker bug for clang-cl support.  If you’d like to help, or have awesome ideas on the possibilities this opens up, please get in touch!

Tagged with: , ,
18 comments on “Building Firefox on Windows with clang-cl
  1. Girish Sharma says:

    Are there any build time comparisons ?

    • ehsan says:

      clang-cl seems to be around 30% slower than MSVC on non-optimized debug builds.

      • Girish Sharma says:

        🙁 – What windows need right now is a better build timings …

        Even on my SSD, build time increased from ~30 min to ~45 min in the last 3-4 months.

  2. Saad says:

    just following Google!

    • ehsan says:

      I’m not sure what you mean, but it’s not very respectful to discredit this effort by calling it “just following Google”. Google is funding the development of clang-cl, and it’s great that they’re doing this, and I expect many software projects will be interested in this.

      • Saad says:

        manzooram ine ke Mozilla khodesh ziad khalaghiat nadare va hamash az google yad migire. az zahere Firefox gerefte ta hasteye Firefox. albate eshkali nadare, amma behtare ke be darkhast va nazare karbarash bishtar tavajjoh kone. masalan chera add-on bar hazf shod. labod mozilla javab mide ke az extension makhsoose in kar estefade konid. amma kheiliha mesle man tarjih midan in vijegi joze khode Firefox bashe va az nasbe extension haye ziad khosheshoon namiad.
        chand mah pish az mozilla khastam ye shortkey vase restart kardane Firefox besazan. amma maskharam kardand! dar soorati ke che bad az nasbe add-on ha va che moghe estefade az VPN in kheili lazem mishe. alan ke extension makhsoose restart kardane Firefox daram, har rooz chandin martabe be dardam mikhore.
        khodam chand ta darkhaste dige ham daram. har che zoodtar Firefox finale 64bit montashere beshe ta barname saritar ejra beshe. ejraye Firefox roo systeme man 4 ya 5 saanie tool mikashe amma male Chrome 1 ta 2 saanie tool mikashe. va ye moshkele digeye Firefox ine ke vaghti kharej shodim bayad chand sanie sabr konim va bad ejra konim, ta error nade. shayad age vase Firefox tahte windows ye win service sakhte beshe, in 2 moshkele firefox hal beshe. yani ham sari ejra beshe va ham bad az baste shodan baraye ejraye mojaddade oon niazi be sabr kardan nabashe.
        yekam negarane Firefox hastam. choon bishtare karbaran daran be samte Chrome miran va mitarsam Firefox roozi vase manam tabdil be khatere beshe. ishalla ke intor namishe.

        • ehsan says:

          فکر نمی‌کنم هیچ کدوم از این موارد به پست من ارتباطی داشته باشه. و تقریباً در هیچ‌کدوم از این موارد تصمیم نهایی با من نیست. در موارد مربوط به طراحی ظاهر کاربری لطفاً توجه کنید که نظر اشخاص مختلف متفاوت هست و هدف طراحی‌های موجود بهتر کردن واسط کاربری برای اکثر کاربران هست، و در این جور موارد امکان راضی کردن همه‌ی کاربران وجود نداره. در مورد موارد تکینیکی که مطرح کردین، محل مناسب برای این بحث باگزیلا هست، و در آنجا افرادی که روی بخش‌های مربوط به مشکلاتی که اشاره کردید کار می‌کنند می‌توانند کمک کنند. در مورد فرضیه‌ی خلاقیت نداشتن ما هم ترجیح می‌دم وارد اون بحث نشم، و با کمال احترام با نظر شما مخالفت کنم. موفق باشید

  3. Bug filed for “AddressSanitizer builds on Windows” yet? 🙂

  4. glandium says:

    If clang-cl can be “installed” anywhere, and can just be unpacked from a tarball, we can add it to tooltool and allow clang-cl builds on try.

  5. Jonny Yu says:

    Is AddressSanitizer available on clang-cl on Windows?

  6. Qb says:

    Very nice! Out of curiosity, on which standard libraries/CRT depend the binaries?

    • ehsan says:

      The Microsoft C and C++ runtime libraries are used in these builds, just like the MSVC builds.