--- ./mesonbuild/backend/ninjabackend.py_orig
+++ ./mesonbuild/backend/ninjabackend.py
@@ -27,6 +27,7 @@ import re
 import shlex
 import subprocess
 import typing as T
+import platform
 
 from . import backends
 from .. import modules
@@ -1026,6 +1027,15 @@ class NinjaBackend(backends.Backend):
         self.generate_dependency_scan_target(target, compiled_sources, source2object, generated_source_files, fortran_order_deps)
         self.add_build(elem)
 
+        #In AIX, we archive shared library. If the instance is a shared library we add a command to archive the shared library 
+        #object and create the build element.  
+        if platform.system().lower() == 'aix' and isinstance(target, build.SharedLibrary):
+          x_outname = outname.replace ('.so', '.a')
+          aix_outname = re.sub('[.][a]([.]?([0-9]+))*([.]?([a-z]+))*','.a',x_outname)
+          aix_final_obj_list = [outname]
+          elem = NinjaBuildElement(self.all_outputs, aix_outname, 'AIX_LINKER', aix_final_obj_list)
+          self.add_build(elem)
+
     def should_use_dyndeps_for_target(self, target: 'build.BuildTarget') -> bool:
         if mesonlib.version_compare(self.ninja_version, '<1.10.0'):
             return False
@@ -1277,6 +1287,11 @@ class NinjaBackend(backends.Backend):
         self.add_rule_comment(NinjaComment('Rules for linking.'))
         self.generate_static_link_rules()
         self.generate_dynamic_link_rules()
+
+        #This adds a AIX Linker rule to archive shared libraries. 
+        if platform.system().lower() == 'aix':
+          self.generate_aix_dynamic_link_rules()
+
         self.add_rule_comment(NinjaComment('Other rules'))
         # Ninja errors out if you have deps = gcc but no depfile, so we must
         # have two rules for custom commands.
@@ -2241,6 +2256,20 @@ class NinjaBackend(backends.Backend):
             options['rspfile_quote_style'] = tool.rsp_file_syntax()
         return options
 
+    def generate_aix_dynamic_link_rules(self):
+        for for_machine in MachineChoice:
+            static_linker = self.build.static_linker[for_machine]
+            rule = 'AIX_LINKER{}'.format(self.get_rule_suffix(for_machine))
+            cmdlist = ['ar']
+            args = ['$in']
+            cmdlist += ['-q', '-v']
+            cmdlist += NinjaCommandArg.list(static_linker.get_output_args('$out'), Quoting.none)
+            #Remove the shared library since it is already there in the archive. 
+            args += ['&&', 'rm', '-f', '$in']
+            description = 'Archiving AIX shared library'
+            options = self._rsp_options(static_linker)
+            self.add_rule(NinjaRule(rule, cmdlist, args, description, **options, extra=None))
+
     def generate_static_link_rules(self):
         num_pools = self.environment.coredata.options[OptionKey('backend_max_links')].value
         if 'java' in self.environment.coredata.compilers.host:
@@ -3336,6 +3365,12 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
         else:
             dependencies = target.get_dependencies()
         internal = self.build_target_link_arguments(linker, dependencies)
+        #In AIX since shared libraries are archived the dependencies must
+        #depend on .a file with the .so and not directly on the .so file.
+        if platform.system().lower() == 'aix':
+            for i, val in enumerate(internal):
+                 internal[i] = internal[i].replace ('.so', '.a')
+                 internal[i] = re.sub ('[.][a]([.]?([0-9]+))*([.]?([a-z]+))*','.a', internal[i]) 
         commands += internal
         # Only non-static built targets need link args and link dependencies
         if not isinstance(target, build.StaticLibrary):
@@ -3536,7 +3571,13 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
             for t in deps.values():
                 # Add the first output of each target to the 'all' target so that
                 # they are all built
-                targetlist.append(os.path.join(self.get_target_dir(t), t.get_outputs()[0]))
+
+                #Add archive file if shared library in AIX for build all.
+                if platform.system().lower() == 'aix':
+                     aix_tmp = re.sub ('[.][a]([.]?([0-9]+))*([.]?([a-z]+))*','.a', t.get_outputs()[0].replace ('.so','.a'))
+                     targetlist.append(os.path.join(self.get_target_dir(t), t.get_outputs()[0] if platform.system().lower() != 'aix' and not(t.get_outputs()[0].endswith('.so')) else aix_tmp))
+                else:
+                     targetlist.append(os.path.join(self.get_target_dir(t), t.get_outputs()[0]))
 
             elem = NinjaBuildElement(self.all_outputs, targ, 'phony', targetlist)
             self.add_build(elem)
-- 
2.38.3
