# Copyright 2013-2019 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 glob
from spack import *


class Leveldb(MakefilePackage):
    """LevelDB is a fast key-value storage library written at Google
    that provides an ordered mapping from string keys to string values."""

    homepage = "https://github.com/google/leveldb"
    url      = "https://github.com/google/leveldb/archive/v1.20.tar.gz"
    git      = "https://github.com/google/leveldb.git"

    version('master', branch='master')
    version('1.20', '298b5bddf12c675d6345784261302252')
    version('1.18', '73770de34a2a5ab34498d2e05b2b7fa0')

    depends_on("snappy")

    def install(self, spec, prefix):
        mkdirp(prefix.lib.pkgconfig)

        libraries  = glob.glob('out-shared/libleveldb.*')
        libraries += glob.glob('out-static/libleveldb.*')
        for library in libraries:
            install(library, prefix.lib)

        install_tree('include', prefix.include)

        with open(join_path(prefix.lib, 'pkgconfig', 'leveldb.pc'), 'w') as f:
            f.write('prefix={0}\n'.format(prefix))
            f.write('exec_prefix=${prefix}\n')
            f.write('libdir={0}\n'.format(prefix.lib))
            f.write('includedir={0}\n'.format(prefix.include))
            f.write('\n')
            f.write('Name: leveldb\n')
            f.write('Description: LevelDB is a fast key-value storage library'
                    ' written at Google that provides an ordered mapping from'
                    ' string keys to string values.\n')
            f.write('Version: {0}\n'.format(spec.version))
            f.write('Cflags: -I${includedir}\n')
            f.write('Libs: -L${libdir} -lleveldb\n')

@@ -243,16 +243,13 @@
 ldb(
+C
 Make
-file
 Pack
@@ -512,13 +512,12 @@
 ive/
-v
 1.2
-0
+2
 .tar
@@ -616,16 +616,104 @@
 aster')%0A
+    version('1.22', '55423cac9e3306f4a9502c738a001e4a339d1a38ffbee7572d4a07d5d63949b2')%0A
     vers
@@ -825,104 +825,743 @@
     
-depends_on(%22snappy%22)%0A%0A    def install(self, spec, prefix):%0A        mkdirp(prefix.lib.pkgconfig)%0A
+variant('shared', default=True, description='Build shared library')%0A%0A    depends_on('cmake@3.9:', when='@1.21:', type='build')%0A%0A    depends_on('snappy')%0A%0A    def url_for_version(self, version):%0A        url = 'https://github.com/google/leveldb/archive/%7B0%7D.tar.gz'%0A%0A        if version %3E= Version('1.21'):%0A            ver = version%0A        else:%0A            ver = 'v%7B0%7D'.format(version)%0A%0A        return url.format(ver)%0A%0A    # CMake support was added in version 1.21%0A    @when('@:1.20')%0A    def cmake(self, spec, prefix):%0A        pass%0A%0A    @when('@:1.20')%0A    def build(self, spec, prefix):%0A        pass%0A%0A    @when('@:1.20')%0A    def install(self, spec, prefix):%0A        make()%0A%0A        mkdirp(prefix.lib)%0A%0A        # Needed for version 1.20
 %0A   
@@ -1665,32 +1665,114 @@
 /libleveldb.*')%0A
+        # Needed for version 1.18%0A        libraries += glob.glob('libleveldb.*')%0A%0A
         for libr
@@ -1888,55 +1888,450 @@
     
-    with open(join_path(prefix.lib, 'pkgconfig'
+def cmake_args(self):%0A        args = %5B%5D%0A%0A        if '+shared' in self.spec:%0A            args.append('-DBUILD_SHARED_LIBS=ON')%0A        else:%0A            args.append('-DBUILD_SHARED_LIBS=OFF')%0A%0A        return args%0A%0A    @run_after('install')%0A    def install_pkgconfig(self):%0A        libdir = self.spec%5B'leveldb'%5D.libs.directories%5B0%5D%0A        pkg_path = join_path(libdir, 'pkgconfig')%0A        mkdirp(pkg_path)%0A%0A        with open(join_path(pkg_path
 , 'l
@@ -2392,24 +2392,29 @@
 %7D%5Cn'.format(
+self.
 prefix))%0A   
@@ -2499,26 +2499,22 @@
 .format(
-prefix.
 lib
+dir
 ))%0A     
@@ -2554,16 +2554,21 @@
 .format(
+self.
 prefix.i
@@ -2899,16 +2899,21 @@
 .format(
+self.
 spec.ver

add 1.22 (#12044)