summaryrefslogtreecommitdiff
path: root/var/spack/repos/builtin/packages/py-tensorflow/package.py
blob: 5551a15b4a006c6f38cc4fe4d6e8b9af69530101 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# Copyright 2013-2024 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import sys
import tempfile

from spack.package import *


class PyTensorflow(Package, CudaPackage, ROCmPackage, PythonExtension):
    """TensorFlow is an open source machine learning framework for everyone."""

    homepage = "https://www.tensorflow.org"
    url = "https://github.com/tensorflow/tensorflow/archive/v2.3.1.tar.gz"

    maintainers("adamjstewart", "aweits")
    import_modules = ["tensorflow"]

    license("Apache-2.0")

    version("2.14.0", sha256="ce357fd0728f0d1b0831d1653f475591662ec5bca736a94ff789e6b1944df19f")
    version("2.13.1", sha256="89c07aebd4f41fbe0d08cc88aef00305542134f2f16d3b62918dc3c1182f33e2")
    version("2.13.0", sha256="e58c939079588623e6fa1d054aec2f90f95018266e0a970fd353a5244f5173dc")
    version("2.12.1", sha256="6bc4600cc0b88e9e40f1800096f5bddbbd3b6e5527a030dea631b87f2ae46b5b")
    version("2.12.0", sha256="c030cb1905bff1d2446615992aad8d8d85cbe90c4fb625cee458c63bf466bc8e")
    version("2.11.1", sha256="624ed1cc170cdcc19e8a15d8cdde989a9a1c6b0534c90b38a6b2f06fb2963e5f")
    version("2.11.0", sha256="99c732b92b1b37fc243a559e02f9aef5671771e272758aa4aec7f34dc92dac48")
    version("2.10.1", sha256="622a92e22e6f3f4300ea43b3025a0b6122f1cc0e2d9233235e4c628c331a94a3")
    version("2.10.0", sha256="b5a1bb04c84b6fe1538377e5a1f649bb5d5f0b2e3625a3c526ff3a8af88633e8")
    version("2.9.3", sha256="59d09bd00eef6f07477eea2f50778582edd4b7b2850a396f1fd0c646b357a573")
    version("2.9.2", sha256="8cd7ed82b096dc349764c3369331751e870d39c86e73bbb5374e1664a59dcdf7")
    version("2.9.1", sha256="6eaf86ead73e23988fe192da1db68f4d3828bcdd0f3a9dc195935e339c95dbdc")
    version("2.9.0", sha256="8087cb0c529f04a4bfe480e49925cd64a904ad16d8ec66b98e2aacdfd53c80ff")
    version("2.8.4", sha256="c08a222792bdbff9da299c7885561ee27b95d414d1111c426efac4ccdce92cde")
    version("2.8.3", sha256="4b7ecbe50b36887e1615bc2a582cb86df1250004d8bb540e18336d539803b5a7")
    version("2.8.2", sha256="b3f860c02c22a30e9787e2548ca252ab289a76b7778af6e9fa763d4aafd904c7")
    version("2.8.1", sha256="4b487a63d6f0c1ca46a2ac37ba4687eabdc3a260c222616fa414f6df73228cec")
    version("2.8.0", sha256="66b953ae7fba61fd78969a2e24e350b26ec116cf2e6a7eb93d02c63939c6f9f7")
    version(
        "2.7.4-rocm-enhanced",
        sha256="45b79c125edfdc008274f1b150d8b5a53b3ff4713fd1ad1ff4738f515aad8191",
        url="https://github.com/ROCmSoftwarePlatform/tensorflow-upstream/archive/refs/tags/v2.7.4-rocm-enhanced.tar.gz",
    )
    version("2.7.4", sha256="75b2e40a9623df32da16d8e97528f5e02e4a958e23b1f2ee9637be8eec5d021b")
    version("2.7.3", sha256="b576c2e124cd6d4d04cbfe985430a0d955614e882172b2258217f0ec9b61f39b")
    version("2.7.2", sha256="b3c8577f3b7cc82368ff7f9315821d506abd2f716ea6692977d255b7d8bc54c0")
    version("2.7.1", sha256="abebe2cf5ca379e18071693ca5f45b88ade941b16258a21cc1f12d77d5387a21")
    version("2.7.0", sha256="bb124905c7fdacd81e7c842b287c169bbf377d29c74c9dacc04f96c9793747bb")
    version("2.6.5", sha256="305da42845ac584a42494e521c92a88ce92ee47d93022d4c0bb45180b5c19a8c")
    version("2.6.4", sha256="6a9e54f46039ef0a6f0a1adf19befa510044d3203d1e124dba8318ec4b1e0210")
    version("2.6.3", sha256="7a71dde0987677b9512b202eb6ae119e0e308b1ea15b66dcfce001a44873997b")
    version("2.6.2", sha256="e68c1d346fc3d529653530ca346b2c62f5b31bd4fcca7ffc9c65bb39ab2f6ed3")
    version("2.6.1", sha256="8e457f617bc2eb43de2a51900e7922b60a8107e2524b2576438f1acccee1d043")
    version("2.6.0", sha256="41b32eeaddcbc02b0583660bcf508469550e4cd0f86b22d2abe72dfebeacde0f")
    version("2.5.3", sha256="58d69b7163f7624debc243750976d27fa7dddbc6fb7c5215aec94732bcc670e1")
    version("2.5.2", sha256="bcccc6ba0b8ac1d10d3302f766eed71911acecc0bc43d0bd27d97a1e7ce275a8")
    version("2.5.1", sha256="8d2728e155a3aa6befd9cb3d0980fabd25e2142d124f8f6b6c78cdf17ff79da5")
    version("2.5.0", sha256="233875ea27fc357f6b714b2a0de5f6ff124b50c1ee9b3b41f9e726e9e677b86c")
    version("2.4.4", sha256="f1abc3ed92c3ce955db2a7db5ec422a3a98f015331183194f97b99fe77a09bb4")
    version("2.4.3", sha256="cafd520c753f8755a9eb1262932f685dc722d8658f08373f8ec88d8acd58d7d4")
    version("2.4.2", sha256="edc88da97277906513d53eeee57997a2036fa32ac1f1937730301764fa06cdc0")
    version("2.4.1", sha256="f681331f8fc0800883761c7709d13cda11942d4ad5ff9f44ad855e9dc78387e0")
    version("2.4.0", sha256="26c833b7e1873936379e810a39d14700281125257ddda8cd822c89111db6f6ae")
    version("2.3.4", sha256="195947838b0918c15d79bc6ed85ff714b24d6d564b4d07ba3de0b745a2f9b656")
    version("2.3.3", sha256="b91e5bcd373b942c4a62c6bcb7ff6f968b1448152b82f54a95dfb0d8fb9c6093")
    version("2.3.2", sha256="21a703d2e68cd0677f6f9ce329198c24fd8203125599d791af9f1de61aadf31f")
    version("2.3.2", sha256="21a703d2e68cd0677f6f9ce329198c24fd8203125599d791af9f1de61aadf31f")
    version("2.3.1", sha256="ee534dd31a811f7a759453567257d1e643f216d8d55a25c32d2fbfff8153a1ac")
    version("2.3.0", sha256="2595a5c401521f20a2734c4e5d54120996f8391f00bb62a57267d930bce95350")
    version("2.2.3", sha256="5e6c779ca8392864d436d88893461dcce783c3a8d46dcb2b2f2ee8ece3cc4538")
    version("2.2.2", sha256="fb4b5d26c5b983350f7ce8297b71176a86a69e91faf66e6ebb1e58538ad3bb51")
    version("2.2.1", sha256="e6a28e64236d729e598dbeaa02152219e67d0ac94d6ed22438606026a02e0f88")
    version("2.2.0", sha256="69cd836f87b8c53506c4f706f655d423270f5a563b76dc1cfa60fbc3184185a3")

    variant("mkl", default=False, description="Build with MKL support")
    variant("jemalloc", default=False, description="Build with jemalloc as malloc support")
    variant("gcp", default=False, description="Build with Google Cloud Platform support")
    variant("hdfs", default=False, description="Build with Hadoop File System support")
    variant("aws", default=False, description="Build with Amazon AWS Platform support")
    variant("xla", default=sys.platform != "darwin", description="Build with XLA JIT support")
    variant("gdr", default=False, description="Build with GDR support")
    variant("verbs", default=False, description="Build with libverbs support")
    variant("ngraph", default=False, description="Build with Intel nGraph support")
    variant("opencl", default=False, description="Build with OpenCL SYCL support")
    variant("computecpp", default=False, description="Build with ComputeCPP support")
    variant(
        "tensorrt", default=False, description="Build with TensorRT support"
    )  # TODO: enable when TensorRT in Spack
    variant("cuda", default=sys.platform != "darwin", description="Build with CUDA support")
    variant(
        "nccl", default=sys.platform.startswith("linux"), description="Enable NVIDIA NCCL support"
    )
    variant("mpi", default=False, description="Build with MPI support")
    variant("android", default=False, description="Configure for Android builds")
    variant("ios", default=False, description="Build with iOS support (macOS only)")
    variant("monolithic", default=False, description="Static monolithic build")
    variant("numa", default=False, description="Build with NUMA support")
    variant(
        "dynamic_kernels",
        default=sys.platform.startswith("linux"),
        description="Build kernels into separate shared objects",
    )

    extends("python")

    # Python support based on wheel availability
    depends_on("python@3.9:3.11", when="@2.14:", type=("build", "run"))
    depends_on("python@3.8:3.11", when="@2.12:2.13", type=("build", "run"))
    depends_on("python@:3.10", when="@2.8:2.11", type=("build", "run"))
    depends_on("python@:3.9", when="@2.5:2.7", type=("build", "run"))
    depends_on("python@:3.8", when="@2.2:2.4", type=("build", "run"))

    # See .bazelversion
    depends_on("bazel@6.1.0", type="build", when="@2.14:")
    depends_on("bazel@5.3.0", type="build", when="@2.11:2.13")
    depends_on("bazel@5.1.1", type="build", when="@2.10")
    # See _TF_MIN_BAZEL_VERSION and _TF_MAX_BAZEL_VERSION in configure.py
    depends_on("bazel@4.2.2:5.99.0", type="build", when="@2.9")
    depends_on("bazel@4.2.1:4.99.0", type="build", when="@2.8")
    depends_on("bazel@3.7.2:4.99.0", type="build", when="@2.7")
    depends_on("bazel@3.7.2:3.99.0", type="build", when="@2.5:2.6")
    depends_on("bazel@3.1.0:3.99.0", type="build", when="@2.3:2.4")
    depends_on("bazel@2.0.0", type="build", when="@2.2")

    depends_on("swig", type="build")
    depends_on("py-pip", type="build")
    depends_on("py-wheel", type="build")

    # Listed under REQUIRED_PACKAGES in tensorflow/tools/pip_package/setup.py
    depends_on("py-absl-py@1:", type=("build", "run"), when="@2.9:")
    depends_on("py-absl-py@0.4:", type=("build", "run"), when="@2.7:2.8")
    depends_on("py-absl-py@0.10:0", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-absl-py@0.7:", type=("build", "run"), when="@:2.3")
    depends_on("py-astunparse@1.6:", type=("build", "run"), when="@2.7:")
    depends_on("py-astunparse@1.6.3:1.6", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-astunparse@1.6.3", type=("build", "run"), when="@2.2:2.3")
    depends_on("py-flatbuffers@23.5.26:", type=("build", "run"), when="@2.14:")
    depends_on("py-flatbuffers@23.1.21:", type=("build", "run"), when="@2.13")
    depends_on("py-flatbuffers@2:", type=("build", "run"), when="@2.10:2.12")
    depends_on("py-flatbuffers@1.12:1", type=("build", "run"), when="@2.9")
    depends_on("py-flatbuffers@1.12:", type=("build", "run"), when="@2.8")
    depends_on("py-flatbuffers@1.12:2", type=("build", "run"), when="@2.7")
    depends_on("py-flatbuffers@1.12", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-gast@0.2.1:0.4,0.5.3:", type=("build", "run"), when="@2.14:")
    depends_on("py-gast@0.2.1:0.4.0", type=("build", "run"), when="@2.9:2.13")
    depends_on("py-gast@0.2.1:", type=("build", "run"), when="@2.8")
    depends_on("py-gast@0.2.1:0.4", type=("build", "run"), when="@2.7")
    depends_on("py-gast@0.4.0", type=("build", "run"), when="@2.5:2.6")
    depends_on("py-gast@0.3.3", type=("build", "run"), when="@2.2:2.4")
    depends_on("py-gast@0.2.2", type=("build", "run"), when="@:2.1")
    depends_on("py-google-pasta@0.1.1:", type=("build", "run"), when="@2.7:")
    depends_on("py-google-pasta@0.2:0", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-google-pasta@0.1.8:", type=("build", "run"), when="@2.2:2.3")
    depends_on("py-google-pasta@0.1.6:", type=("build", "run"), when="@:2.1")
    depends_on("py-h5py@2.9:", type=("build", "run"), when="@2.7:")
    depends_on("py-h5py@3.1", type=("build", "run"), when="@2.5:2.6")
    depends_on("py-h5py@2.10", type=("build", "run"), when="@2.2:2.4")
    depends_on("py-h5py@:2.10.0", type=("build", "run"), when="@2.1.3:2.1")
    # propagate the mpi variant setting for h5py/hdf5 to avoid unexpected crashes
    depends_on("py-h5py+mpi", type=("build", "run"), when="@2.1.3:+mpi")
    depends_on("py-h5py~mpi", type=("build", "run"), when="@2.1.3:~mpi")
    depends_on("hdf5+mpi", type="build", when="@2.1.3:+mpi")
    depends_on("hdf5~mpi", type="build", when="@2.1.3:~mpi")
    depends_on("py-libclang@13:", type=("build", "run"), when="@2.9:")
    depends_on("py-libclang@9.0.1:", type=("build", "run"), when="@2.7:2.8")
    depends_on("py-ml-dtypes@0.2.0", type=("build", "run"), when="@2.14:")
    depends_on("py-numpy@1.23.5:", type=("build", "run"), when="@2.14:")
    depends_on("py-numpy@1.22:1.24.3", type=("build", "run"), when="@2.13:")
    depends_on("py-numpy@1.22:1.23", type=("build", "run"), when="@2.12")
    depends_on("py-numpy@1.20:", type=("build", "run"), when="@2.8:2.11")
    depends_on("py-numpy@1.14.5:", type=("build", "run"), when="@2.7")
    depends_on("py-numpy@1.19.2:1.19", type=("build", "run"), when="@2.4:2.6")
    # https://github.com/tensorflow/tensorflow/issues/40688
    depends_on("py-numpy@1.16.0:1.18", type=("build", "run"), when="@:2.3")
    depends_on("py-opt-einsum@2.3.2:", type=("build", "run"), when="@:2.3,2.7:")
    depends_on("py-opt-einsum@3.3", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-packaging", type=("build", "run"), when="@2.9:")
    depends_on("py-protobuf@3.20.3:4.20,4.21.6:4", type=("build", "run"), when="@2.12:")
    depends_on("py-protobuf@3.9.2:", type=("build", "run"), when="@2.3:2.11")
    depends_on("py-protobuf@3.8.0:", type=("build", "run"), when="@:2.2")
    # https://github.com/protocolbuffers/protobuf/issues/10051
    # https://github.com/tensorflow/tensorflow/issues/56266
    depends_on("py-protobuf@:3.19", type=("build", "run"), when="@:2.11")
    # Must be matching versions of py-protobuf and protobuf
    conflicts("^py-protobuf~cpp")
    depends_on("py-setuptools", type=("build", "run"))
    depends_on("py-six@1.12:", type=("build", "run"), when="@:2.3,2.7:")
    depends_on("py-six@1.15", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-termcolor@1.1:", type=("build", "run"), when="@:2.3,2.7:")
    depends_on("py-termcolor@1.1", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-typing-extensions@3.6.6:", type=("build", "run"), when="@2.7:2.12,2.14:")
    depends_on("py-typing-extensions@3.6.6:4.5", type=("build", "run"), when="@2.13")
    depends_on("py-typing-extensions@3.7.4:3.7", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-wrapt@1.11:1.14", type=("build", "run"), when="@2.12,2.14:")
    depends_on("py-wrapt@1.11:", type=("build", "run"), when="@2.7:2.11,2.13")
    depends_on("py-wrapt@1.12.1:1.12", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-wrapt@1.11.1:", type=("build", "run"), when="@:2.3")

    # TODO: add packages for these dependencies
    # depends_on('py-tensorflow-io-gcs-filesystem@0.23.1:', type=('build', 'run'), when='@2.8:')
    # depends_on('py-tensorflow-io-gcs-filesystem@0.21:', type=('build', 'run'), when='@2.7')

    if sys.byteorder == "little":
        # Only builds correctly on little-endian machines
        depends_on("py-grpcio@1.24.3:1", type=("build", "run"), when="@2.7:")
        depends_on("py-grpcio@1.37.0:1", type=("build", "run"), when="@2.6")
        depends_on("py-grpcio@1.34", type=("build", "run"), when="@2.5")
        depends_on("py-grpcio@1.32", type=("build", "run"), when="@2.4")
        depends_on("py-grpcio@1.8.6:", type=("build", "run"), when="@:2.3")

    for minor_ver in range(5, 15):
        depends_on(
            "py-tensorboard@2.{}".format(minor_ver),
            type=("build", "run"),
            when="@2.{}".format(minor_ver),
        )
    # TODO: is this still true? We now install tensorboard from wheel for all versions
    # depends_on('py-tensorboard', when='@:2.4')  # circular dep
    # depends_on('py-tensorflow-estimator')  # circular dep
    # depends_on('py-keras')  # circular dep

    # Undocumented dependencies
    depends_on("py-requests", type=("build", "run"))
    # https://github.com/tensorflow/tensorflow/issues/60179#issuecomment-1491238631
    depends_on("coreutils", when="@2.13: platform=darwin", type="build")

    # No longer a dependency in latest versions
    depends_on("py-jax@0.3.15:", type=("build", "run"), when="@2.12")
    depends_on("py-keras-preprocessing@1.1.1:", type=("build", "run"), when="@2.7:2.10")
    depends_on("py-keras-preprocessing@1.1.2:1.1", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-keras-preprocessing@1.1.1:1.1", type=("build", "run"), when="@2.3")
    depends_on("py-keras-preprocessing@1.1:", type=("build", "run"), when="@2.2")
    depends_on("py-scipy@1.4.1", type=("build", "run"), when="@2.2.0,2.3.0")
    depends_on("py-wheel@0.32:0", type=("build", "run"), when="@2.7")
    depends_on("py-wheel@0.35:0", type=("build", "run"), when="@2.4:2.6")
    depends_on("py-wheel@0.26:", type=("build", "run"), when="@:2.3")

    # TODO: add packages for some of these dependencies
    depends_on("mkl", when="+mkl")
    depends_on("curl", when="+gcp")
    # depends_on('computecpp', when='+opencl+computecpp')
    # depends_on('trisycl',    when='+opencl~computepp')
    depends_on("cuda@:10.2", when="+cuda @:2.3")
    depends_on("cuda@:11.4", when="+cuda @2.4:2.7")
    # avoid problem fixed by commit a76f797b9cd4b9b15bec4c503b16236a804f676f
    depends_on("cuda@:11.7.0", when="+cuda @:2.9")
    depends_on("cudnn", when="+cuda")
    depends_on("cudnn@:7", when="@:2.2 +cuda")
    # depends_on('tensorrt', when='+tensorrt')
    depends_on("nccl", when="+nccl+cuda")
    depends_on("mpi", when="+mpi")
    # depends_on('android-ndk@10:18', when='+android')
    # depends_on('android-sdk', when='+android')

    with when("+rocm"):
        depends_on("hip")
        depends_on("rocrand")
        depends_on("rocblas")
        depends_on("rocfft")
        depends_on("hipfft")
        depends_on("rccl", when="+nccl")
        depends_on("hipsparse")
        depends_on("hipcub")
        depends_on("rocsolver")
        depends_on("rocprim")
        depends_on("miopen-hip")
        depends_on("llvm-amdgpu")
        depends_on("hsa-rocr-dev")
        depends_on("rocminfo")

    # Check configure and configure.py to see when these variants are supported
    conflicts("+mkl", when="platform=darwin", msg="Darwin is not yet supported")
    conflicts(
        "+jemalloc",
        when="platform=darwin",
        msg="Currently jemalloc is only support on Linux platform",
    )
    conflicts(
        "+jemalloc",
        when="platform=cray",
        msg="Currently jemalloc is only support on Linux platform",
    )
    conflicts("+opencl", when="platform=windows")
    conflicts("+computecpp", when="~opencl")
    conflicts(
        "+cuda",
        when="+rocm",
        msg="CUDA / ROCm are mututally exclusive. At most 1 GPU platform can be configured",
    )
    conflicts("+cuda", when="platform=darwin", msg="There is no GPU support for macOS")
    conflicts(
        "cuda_arch=none",
        when="+cuda",
        msg="Must specify CUDA compute capabilities of your GPU, see https://developer.nvidia.com/cuda-gpus",
    )
    conflicts("cuda_arch=20", msg="TensorFlow only supports compute capabilities >= 3.5")
    conflicts("cuda_arch=30", msg="TensorFlow only supports compute capabilities >= 3.5")
    conflicts("cuda_arch=32", msg="TensorFlow only supports compute capabilities >= 3.5")
    conflicts("+tensorrt", when="~cuda")
    conflicts(
        "+tensorrt",
        when="platform=darwin",
        msg="Currently TensorRT is only supported on Linux platform",
    )
    conflicts(
        "+tensorrt",
        when="platform=cray",
        msg="Currently TensorRT is only supported on Linux platform",
    )
    conflicts("+nccl", when="~cuda~rocm")
    conflicts(
        "+nccl", when="platform=darwin", msg="Currently NCCL is only supported on Linux platform"
    )
    conflicts(
        "+nccl", when="platform=cray", msg="Currently NCCL is only supported on Linux platform"
    )
    conflicts("+mpi", when="platform=windows")
    conflicts("+ios", when="platform=linux", msg="iOS support only available on macOS")
    conflicts("+ios", when="platform=cray", msg="iOS support only available on macOS")
    # https://github.com/tensorflow/tensorflow/pull/45404
    conflicts("platform=darwin target=aarch64:", when="@:2.4")
    # https://github.com/tensorflow/tensorflow/pull/39225
    conflicts("target=aarch64:", when="@:2.2")
    conflicts("~rocm", when="@2.7.4-rocm-enhanced")
    conflicts("+rocm", when="@:2.7.4-a,2.7.4.0:")

    # wheel 0.40 upgrades vendored packaging, trips over tensorflow-io-gcs-filesystem identifier
    conflicts("^py-wheel@0.40:", when="@2.11:2.13")

    # https://www.tensorflow.org/install/source#tested_build_configurations
    conflicts("%gcc@:9.3.0", when="@2.9:")
    conflicts("%gcc@:7.3.0")

    # zlib is vendored and downloaded directly from zlib.org (or mirrors), but
    # old downloads are removed from that site immediately after a new release.
    # If the tf mirrors don't work, make sure the fallback is to something existing.
    patch(
        "https://github.com/tensorflow/tensorflow/commit/76b9fa22857148a562f3d9b5af6843402a93c15b.patch?full_index=1",
        sha256="f9e26c544da729cfd376dbd3b096030e3777d3592459add1f3c78b1b9828d493",
        when="@2.9:2.10.0",
    )

    # Version 2.10 produces an error related to cuBLAS:
    # E tensorflow/stream_executor/cuda/cuda_blas.cc:2981] Unable to register
    # cuBLAS factory: Attempting to register factory for plugin cuBLAS when one
    # has already been registered
    # See https://github.com/tensorflow/tensorflow/issues/57663
    # This is fixed for 2.11 but 2.10 needs the following patch.
    patch(
        "https://github.com/tensorflow/tensorflow/pull/56691.patch?full_index=1",
        sha256="d635ea6d6c1571505871d0caba3e2cd939ea0f4aff972095d552913a8109def3",
        when="@2.10",
    )

    # needed for protobuf 3.16+
    patch("example_parsing.patch", when="@:2.7 ^protobuf@3.16:")

    # allow linker to be found in PATH
    # https://github.com/tensorflow/tensorflow/issues/39263
    patch("null_linker_bin_path.patch", when="@2.5:")

    # Reset import order to that of 2.4. Part of
    # https://bugs.gentoo.org/800824#c3 From the patch:
    # When tensorflow and python protobuf use the same instance of libprotobuf,
    # pywrap_tensorflow must be imported before anything else that would import
    # protobuf definitions.
    patch("0008-Fix-protobuf-errors-when-using-system-protobuf.patch", when="@2.5:2.6")

    phases = ["configure", "build", "install"]

    # https://www.tensorflow.org/install/source
    def setup_build_environment(self, env):
        spec = self.spec

        # Please specify the location of python
        env.set("PYTHON_BIN_PATH", spec["python"].command.path)

        # Please input the desired Python library path to use
        env.set("PYTHON_LIB_PATH", python_platlib)
        env.set("TF_PYTHON_VERSION", spec["python"].version.up_to(2))

        # Ensure swig is in PATH or set SWIG_PATH
        env.set("SWIG_PATH", spec["swig"].prefix.bin.swig)

        # Do you wish to build TensorFlow with MKL support?
        if "+mkl" in spec:
            env.set("TF_NEED_MKL", "1")

            # Do you wish to download MKL LIB from the web?
            env.set("TF_DOWNLOAD_MKL", "0")

            # Please specify the location where MKL is installed
            env.set("MKL_INSTALL_PATH", spec["mkl"].prefix)
        else:
            env.set("TF_NEED_MKL", "0")

        # Do you wish to build TensorFlow with jemalloc as malloc support?
        if "+jemalloc" in spec:
            env.set("TF_NEED_JEMALLOC", "1")
        else:
            env.set("TF_NEED_JEMALLOC", "0")

        # Do you wish to build TensorFlow with Google Cloud Platform support?
        if "+gcp" in spec:
            env.set("TF_NEED_GCP", "1")
        else:
            env.set("TF_NEED_GCP", "0")

        # Do you wish to build TensorFlow with Hadoop File System support?
        if "+hdfs" in spec:
            env.set("TF_NEED_HDFS", "1")
        else:
            env.set("TF_NEED_HDFS", "0")

        # Do you wish to build TensorFlow with Amazon AWS Platform support?
        if "+aws" in spec:
            env.set("TF_NEED_AWS", "1")
            env.set("TF_NEED_S3", "1")
        else:
            env.set("TF_NEED_AWS", "0")
            env.set("TF_NEED_S3", "0")

        # Do you wish to build TensorFlow with XLA JIT support?
        if "+xla" in spec:
            env.set("TF_ENABLE_XLA", "1")
        else:
            env.set("TF_ENABLE_XLA", "0")

        # Do you wish to build TensorFlow with GDR support?
        if "+gdr" in spec:
            env.set("TF_NEED_GDR", "1")
        else:
            env.set("TF_NEED_GDR", "0")

        # Do you wish to build TensorFlow with VERBS support?
        if "+verbs" in spec:
            env.set("TF_NEED_VERBS", "1")
        else:
            env.set("TF_NEED_VERBS", "0")

        # Do you wish to build TensorFlow with nGraph support?
        if "+ngraph" in spec:
            env.set("TF_NEED_NGRAPH", "1")
        else:
            env.set("TF_NEED_NGRAPH", "0")

        # Do you wish to build TensorFlow with OpenCL SYCL support?
        if "+opencl" in spec:
            env.set("TF_NEED_OPENCL_SYCL", "1")
            env.set("TF_NEED_OPENCL", "1")

            # Please specify which C++ compiler should be used as the host
            # C++ compiler
            env.set("HOST_CXX_COMPILER", spack_cxx)

            # Please specify which C compiler should be used as the host
            # C compiler
            env.set("HOST_C_COMPILER", spack_cc)

            # Do you wish to build TensorFlow with ComputeCPP support?
            if "+computecpp" in spec:
                env.set("TF_NEED_COMPUTECPP", "1")

                # Please specify the location where ComputeCpp is installed
                env.set("COMPUTECPP_TOOLKIT_PATH", spec["computecpp"].prefix)
            else:
                env.set("TF_NEED_COMPUTECPP", "0")

                # Please specify the location of the triSYCL include directory
                env.set("TRISYCL_INCLUDE_DIR", spec["trisycl"].prefix.include)
        else:
            env.set("TF_NEED_OPENCL_SYCL", "0")
            env.set("TF_NEED_OPENCL", "0")

        # Do you wish to build TensorFlow with ROCm support?
        if "+rocm" in spec:
            env.set("TF_NEED_ROCM", "1")
        else:
            env.set("TF_NEED_ROCM", "0")

        # Do you wish to build TensorFlow with CUDA support?
        if "+cuda" in spec:
            env.set("TF_NEED_CUDA", "1")

            # Do you want to use clang as CUDA compiler?
            env.set("TF_CUDA_CLANG", "0")

            # Please specify which gcc nvcc should use as the host compiler
            env.set("GCC_HOST_COMPILER_PATH", spack_cc)

            cuda_paths = [spec["cuda"].prefix, spec["cudnn"].prefix]

            # Do you wish to build TensorFlow with TensorRT support?
            if "+tensorrt" in spec:
                env.set("TF_NEED_TENSORRT", "1")

                cuda_paths.append(spec["tensorrt"].prefix)

                # Please specify the TensorRT version you want to use
                env.set("TF_TENSORRT_VERSION", spec["tensorrt"].version.up_to(1))

                # Please specify the location where TensorRT is installed
                env.set("TENSORRT_INSTALL_PATH", spec["tensorrt"].prefix)
            else:
                env.set("TF_NEED_TENSORRT", "0")
                env.unset("TF_TENSORRT_VERSION")

            # Please specify the CUDA SDK version you want to use
            env.set("TF_CUDA_VERSION", spec["cuda"].version.up_to(2))

            # Please specify the cuDNN version you want to use
            env.set("TF_CUDNN_VERSION", spec["cudnn"].version.up_to(1))

            if "+nccl" in spec:
                cuda_paths.append(spec["nccl"].prefix)

                # Please specify the locally installed NCCL version to use
                env.set("TF_NCCL_VERSION", spec["nccl"].version.up_to(1))

                # Please specify the location where NCCL is installed
                env.set("NCCL_INSTALL_PATH", spec["nccl"].prefix)
                env.set("NCCL_HDR_PATH", spec["nccl"].prefix.include)
            else:
                env.unset("TF_NCCL_VERSION")

            # Please specify the comma-separated list of base paths to
            # look for CUDA libraries and headers
            env.set("TF_CUDA_PATHS", ",".join(cuda_paths))

            # Please specify the location where CUDA toolkit is installed
            env.set("CUDA_TOOLKIT_PATH", spec["cuda"].prefix)

            # Please specify the location where CUDNN library is installed
            env.set("CUDNN_INSTALL_PATH", spec["cudnn"].prefix)

            # Please specify a list of comma-separated CUDA compute
            # capabilities you want to build with. You can find the compute
            # capability of your device at:
            # https://developer.nvidia.com/cuda-gpus.
            # Please note that each additional compute capability significantly
            # increases your build time and binary size, and that TensorFlow
            # only supports compute capabilities >= 3.5
            capabilities = ",".join(
                "{0:.1f}".format(float(i) / 10.0) for i in spec.variants["cuda_arch"].value
            )
            env.set("TF_CUDA_COMPUTE_CAPABILITIES", capabilities)
        else:
            env.set("TF_NEED_CUDA", "0")

        # Do you want to use Clang to build TensorFlow?
        if "%clang" in spec:
            env.set("TF_NEED_CLANG", "1")
        else:
            env.set("TF_NEED_CLANG", "0")

        # Do you wish to download a fresh release of clang? (Experimental)
        env.set("TF_DOWNLOAD_CLANG", "0")

        # Do you wish to build TensorFlow with MPI support?
        if "+mpi" in spec:
            env.set("TF_NEED_MPI", "1")

            # Please specify the MPI toolkit folder
            env.set("MPI_HOME", spec["mpi"].prefix)
        else:
            env.set("TF_NEED_MPI", "0")
            env.unset("MPI_HOME")

        # Please specify optimization flags to use during compilation when
        # bazel option '--config=opt' is specified
        env.set(
            "CC_OPT_FLAGS",
            spec.target.optimization_flags(spec.compiler.name, spec.compiler.version),
        )

        # Would you like to interactively configure ./WORKSPACE for
        # Android builds?
        if "+android" in spec:
            env.set("TF_SET_ANDROID_WORKSPACE", "1")

            # Please specify the home path of the Android NDK to use
            env.set("ANDROID_NDK_HOME", spec["android-ndk"].prefix)
            env.set("ANDROID_NDK_API_LEVEL", spec["android-ndk"].version)

            # Please specify the home path of the Android SDK to use
            env.set("ANDROID_SDK_HOME", spec["android-sdk"].prefix)
            env.set("ANDROID_SDK_API_LEVEL", spec["android-sdk"].version)

            # Please specify the Android SDK API level to use
            env.set("ANDROID_API_LEVEL", spec["android-sdk"].version)

            # Please specify an Android build tools version to use
            env.set("ANDROID_BUILD_TOOLS_VERSION", spec["android-sdk"].version)
        else:
            env.set("TF_SET_ANDROID_WORKSPACE", "0")

        # Do you wish to build TensorFlow with iOS support?
        if "+ios" in spec:
            env.set("TF_CONFIGURE_IOS", "1")
        else:
            env.set("TF_CONFIGURE_IOS", "0")

        # set tmpdir to a non-NFS filesystem
        # (because bazel uses ~/.cache/bazel)
        # TODO: This should be checked for non-nfsy filesystem, but the current
        #       best idea for it is to check
        #           subprocess.call([
        #               'stat', '--file-system', '--format=%T', tmp_path
        #       ])
        #       to not be nfs. This is only valid for Linux and we'd like to
        #       stay at least also OSX compatible
        tmp_path = tempfile.mkdtemp(prefix="spack")
        env.set("TEST_TMPDIR", tmp_path)

    def configure(self, spec, prefix):
        # NOTE: configure script is interactive. If you set the appropriate
        # environment variables, this interactivity is skipped. If you don't,
        # Spack hangs during the configure phase. Use `spack build-env` to
        # determine which environment variables must be set for a particular
        # version.
        configure()

    @run_after("configure")
    def post_configure_fixes(self):
        spec = self.spec

        # make sure xla is actually turned off
        if spec.satisfies("~xla"):
            filter_file(
                r"--define with_xla_support=true",
                r"--define with_xla_support=false",
                ".tf_configure.bazelrc",
            )

        if spec.satisfies("~android"):
            # env variable is somehow ignored -> brute force
            # TODO: find a better solution
            filter_file(r"if workspace_has_any_android_rule\(\)", r"if True", "configure.py")

        if spec.satisfies("~gcp"):
            # google cloud support seems to be installed on default, leading
            # to boringssl error manually set the flag to false to avoid
            # installing gcp support
            # https://github.com/tensorflow/tensorflow/issues/20677#issuecomment-404634519
            filter_file(
                r"--define with_gcp_support=true",
                r"--define with_gcp_support=false",
                ".tf_configure.bazelrc",
            )

        if spec.satisfies("~opencl"):
            # 1.8.0 and 1.9.0 aborts with numpy import error during python_api
            # generation somehow the wrong PYTHONPATH is used...
            # set --distinct_host_configuration=false as a workaround
            # https://github.com/tensorflow/tensorflow/issues/22395#issuecomment-431229451
            with open(".tf_configure.bazelrc", mode="a") as f:
                f.write("build --distinct_host_configuration=false\n")
                f.write('build --action_env PYTHONPATH="{0}"\n'.format(env["PYTHONPATH"]))

        if spec.satisfies("+cuda"):
            libs = spec["cuda"].libs.directories
            libs.extend(spec["cudnn"].libs.directories)
            if "+nccl" in spec:
                libs.extend(spec["nccl"].libs.directories)

            if "+tensorrt" in spec:
                libs.extend(spec["tensorrt"].libs.directories)
            slibs = ":".join(libs)

            with open(".tf_configure.bazelrc", mode="a") as f:
                f.write('build --action_env LD_LIBRARY_PATH="' + slibs + '"')

        filter_file("build:opt --copt=-march=native", "", ".tf_configure.bazelrc")
        filter_file("build:opt --host_copt=-march=native", "", ".tf_configure.bazelrc")

    def build(self, spec, prefix):
        # Bazel needs the directory to exist on install
        mkdirp(python_platlib)
        tmp_path = env["TEST_TMPDIR"]

        # https://docs.bazel.build/versions/master/command-line-reference.html
        args = [
            # Don't allow user or system .bazelrc to override build settings
            "--nohome_rc",
            "--nosystem_rc",
            # Bazel does not work properly on NFS, switch to /tmp
            "--output_user_root=" + tmp_path,
            "build",
            # Spack logs don't handle colored output well
            "--color=no",
            "--jobs={0}".format(make_jobs),
            "--config=opt",
            # Enable verbose output for failures
            "--verbose_failures",
        ]

        if spec.satisfies("^bazel@:3.5"):
            # removed in bazel 3.6
            args.append("--incompatible_no_support_tools_in_action_inputs=false")

        # See .bazelrc for when each config flag is supported
        if "+mkl" in spec:
            args.append("--config=mkl")

        if "+monolithic" in spec:
            args.append("--config=monolithic")

        if "+gdr" in spec:
            args.append("--config=gdr")

        if "+verbs" in spec:
            args.append("--config=verbs")

        if "+ngraph" in spec:
            args.append("--config=ngraph")

        if "+dynamic_kernels" in spec:
            args.append("--config=dynamic_kernels")

        if "+cuda" in spec:
            args.append("--config=cuda")

        if "+rocm" in spec:
            args.append("--config=rocm")

        if "~aws" in spec:
            args.append("--config=noaws")

        if "~gcp" in spec:
            args.append("--config=nogcp")

        if "~hdfs" in spec:
            args.append("--config=nohdfs")

        if "~nccl" in spec:
            args.append("--config=nonccl")

        if "+numa" in spec:
            args.append("--config=numa")

        args.append("--config=v2")

        args.append("//tensorflow/tools/pip_package:build_pip_package")

        bazel(*args)

        build_pip_package = Executable("bazel-bin/tensorflow/tools/pip_package/build_pip_package")
        buildpath = join_path(self.stage.source_path, "spack-build")
        build_pip_package("--src", buildpath)

    def install(self, spec, prefix):
        tmp_path = env["TEST_TMPDIR"]
        buildpath = join_path(self.stage.source_path, "spack-build")
        with working_dir(buildpath):
            args = std_pip_args + ["--prefix=" + prefix, "."]
            pip(*args)
        remove_linked_tree(tmp_path)